手写签名图片处理-Android
生活随笔
收集整理的這篇文章主要介紹了
手写签名图片处理-Android
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
背景
用戶通過(guò)筆在紙上手寫(xiě)了個(gè)人簽名,通過(guò)拍照上傳的方式將其筆跡設(shè)置為簽名圖片。
如果直接使用此圖片(包括裁剪后的圖片),則在簽名的過(guò)程中會(huì)簽名圖案中不但有用戶的筆跡,還有紙的顏色背景,效果堪憂。
解決目標(biāo)
將用戶的手寫(xiě)筆跡采集,并且背景色是透明的
解決思路
-將圖片中的黑色像素點(diǎn)1 保留,其他像素點(diǎn)設(shè)置為透明 (難點(diǎn)和重點(diǎn):哪些色值可以被認(rèn)定為筆跡、用戶拍照時(shí)候的光線影響、用戶手寫(xiě)紙的背景色)
-將圖片保存為手寫(xiě)區(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* @版權(quán)所有: 北京數(shù)字認(rèn)證股份有限公司 (C) 2020** @類描述: 手寫(xiě)筆跡處理* @版本: V4.0.0* @作者 daizhenhong* @創(chuàng)建時(shí)間 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;/*** 將已經(jīng)裁剪了的手寫(xiě)簽名筆跡圖片進(jìn)行透明化處理** @param context* @param imagePath 圖片的文件地址(前綴是file://)* @return 經(jīng)過(guò)背景透明化處理后的圖片地址(需要添加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();}/*** 將圖片進(jìn)行背景透明化處理** @param originBitmap 原始圖片的bitmap對(duì)象* @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();// 由于圖片經(jīng)過(guò)裁剪后是jpeg格式的,如果直接保存成png圖片,會(huì)造成透明的背景色變成黑色(因?yàn)閖peg圖片沒(méi)有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;}/*** 獲取像素點(diǎn)需要變更的顏色* 當(dāng)像素點(diǎn)不是黑色的,則將其設(shè)置為透明** @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-認(rèn)定為黑色筆跡*/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;}}黑色像素點(diǎn)我們的計(jì)算方式:圖像的像素有rgb(jpeg圖片)或rgba組件(png圖片),我們目前的設(shè)定是:rgb的最大一項(xiàng)不大于145 ,同時(shí)rgb之前的最大差距不能超過(guò)14 ??
總結(jié)
以上是生活随笔為你收集整理的手写签名图片处理-Android的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 53-抽屉网
- 下一篇: android 日历动画效果,Andro