手写签名图片处理-Android
生活随笔
收集整理的這篇文章主要介紹了
手写签名图片处理-Android
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
背景
用戶通過筆在紙上手寫了個人簽名,通過拍照上傳的方式將其筆跡設置為簽名圖片。
如果直接使用此圖片(包括裁剪后的圖片),則在簽名的過程中會簽名圖案中不但有用戶的筆跡,還有紙的顏色背景,效果堪憂。
解決目標
將用戶的手寫筆跡采集,并且背景色是透明的
解決思路
-將圖片中的黑色像素點1 保留,其他像素點設置為透明 (難點和重點:哪些色值可以被認定為筆跡、用戶拍照時候的光線影響、用戶手寫紙的背景色)
-將圖片保存為手寫區(qū)域
Android模塊的核心代碼(圖片裁剪不在此范圍)
package cn.org.bjca.wcert.ywq.utils;import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.os.Environment;import java.io.File; import java.io.FileOutputStream;/************************************************************************************************** <pre>* @包路徑: cn.org.bjca.wcert.ywq.utils* @版權所有: 北京數字認證股份有限公司 (C) 2020** @類描述: 手寫筆跡處理* @版本: V4.0.0* @作者 daizhenhong* @創(chuàng)建時間 2020/11/2 3:50 PM </pre>************************************************************************************************/ public class HandSignUtil {final static String filePathHeader = "file://";private final static int maxColorBlack = 145;private final static int mColorDifMax = 14;/*** 將已經裁剪了的手寫簽名筆跡圖片進行透明化處理** @param context* @param imagePath 圖片的文件地址(前綴是file://)* @return 經過背景透明化處理后的圖片地址(需要添加file://協(xié)議頭)* @throws Exception*/public static String getHandSignByImagePath(Context context, String imagePath) throws Exception {String path = imagePath.substring(filePathHeader.length());Bitmap originBitmap = BitmapFactory.decodeFile(path);Bitmap translateBitmap = translateBitmap(originBitmap);String filePath = getSaveImagePath(context);File saveFile = new File(filePath);translateBitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(saveFile));return filePathHeader + saveFile.toString();}private static String getSaveImagePath(Context context) {String cachePath = "";if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())|| !Environment.isExternalStorageRemovable()) {cachePath = context.getExternalCacheDir().getPath();} else {cachePath = context.getCacheDir().getPath();}File fileParent = new File(cachePath);if (!fileParent.exists()) {fileParent.mkdirs();}File path = new File(cachePath, System.currentTimeMillis() + ".png");return path.toString();}/*** 將圖片進行背景透明化處理** @param originBitmap 原始圖片的bitmap對象* @return 背景透明化處理后的bitmap*/private static Bitmap translateBitmap(Bitmap originBitmap) {Bitmap translateBitmap = originBitmap.copy(Bitmap.Config.ARGB_8888, true);int nWidth = translateBitmap.getWidth();int nHeight = translateBitmap.getHeight();// 由于圖片經過裁剪后是jpeg格式的,如果直接保存成png圖片,會造成透明的背景色變成黑色(因為jpeg圖片沒有alpha通道)Bitmap resultBitmap = Bitmap.createBitmap(nWidth, nHeight, Bitmap.Config.ARGB_8888);for (int y = 0; y < nHeight; ++y)for (int x = 0; x < nWidth; ++x) {int nPixelColor = originBitmap.getPixel(x, y);int newColor = getNewColor(nPixelColor);resultBitmap.setPixel(x, y, newColor);}return resultBitmap;}/*** 獲取像素點需要變更的顏色* 當像素點不是黑色的,則將其設置為透明** @param nPixelColor* @return*/private static int getNewColor(int nPixelColor) {if (isBlackColor(nPixelColor)) {return Color.argb(Color.alpha(nPixelColor), Color.red(nPixelColor), Color.green(nPixelColor), Color.blue(nPixelColor));}return Color.TRANSPARENT;}/*** 判斷是否是黑色筆跡** @param color 顏色的int值* @return true-認定為黑色筆跡*/private static boolean isBlackColor(int color) {int r = Color.red(color);int g = Color.green(color);int b = Color.blue(color);int colorMax = Math.max(Math.max(r, g), b);int colorMin = Math.min(Math.min(r, g), b);int dif = colorMax - colorMin;return colorMax < maxColorBlack && dif <= mColorDifMax;}}黑色像素點我們的計算方式:圖像的像素有rgb(jpeg圖片)或rgba組件(png圖片),我們目前的設定是:rgb的最大一項不大于145 ,同時rgb之前的最大差距不能超過14 ??
總結
以上是生活随笔為你收集整理的手写签名图片处理-Android的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 53-抽屉网
- 下一篇: 表单提交成功如何弹出提示