Scanner
識別庫,識別器可選擇,這里有你常用的二維碼/條碼識別,還有你可能用到的身份證、銀行卡識別,如果沒有你想要的,可以自定義識別器。
該庫只識別掃描框內的圖像,識別速率上大大提高,而且這個庫比起其它的庫就是解決了攝像頭預覽變形,預覽頁面高度自定義,你可以像常規一樣整個頁面都是預覽,或者你可以選擇在任何位置定義任何尺寸的預覽,掃描框也高度自定義,你可以像常規一樣居中,或者你也可以在預覽的任何位置定義任何尺寸的掃描框(實際識別的掃描框和畫上去的掃描框不一定是一樣的,由你自己決定)。
效果圖
依賴
implementation 'com.shouzhong:Scanner:1.0.1'
以下選擇自己需要的
// zxing implementation 'com.google.zxing:core:3.3.3' // zbar implementation 'com.shouzhong:ScannerZBarLib:1.0.0' // 銀行卡識別 implementation 'com.shouzhong:ScannerBankCardLib:1.0.0' // 身份證識別 implementation 'com.shouzhong:ScannerIdCardLib:1.0.1'
代碼
基本使用
<RelativeLayout xmlns:Android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.shouzhong.scanner.ScannerView android:id="@+id/sv" android:layout_width="match_parent" android:layout_height="1080px" android:background="#000000"/> </RelativeLayout> @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_scanner); scannerView = findViewById(R.id.sv); scannerView.setViewFinder(new ViewFinder(this)); scannerView.setSaveBmp(true); scannerView.setEnableZXing(true); scannerView.setEnableZBar(true); scannerView.setEnableBankCard(true); scannerView.setEnableIdCard(true); scannerView.setCallback(new Callback() { @Override public void result(Result result) { tvResult.setText("識別結果:n" + result.toString()); scannerView.restartPreviewAfterDelay(2000); } }); } @Override protected void onResume() { super.onResume(); scannerView.onResume(); } @Override protected void onPause() { super.onPause(); scannerView.onPause(); }
開啟或者關閉某個識別器
// 啟用zxing識別器 scannerView.setEnableZXing(true); // 啟用zbar識別器 scannerView.setEnableZBar(true); // 啟用銀行卡識別器 scannerView.setEnableBankCard(true); // 啟用身份證識別器(這里只支持2代身份證) scannerView.setEnableIdCard(true);
如果你想自定義識別器
scannerView.setScanner(new IScanner() { /** * 這里實現自己的識別器,并把識別結果返回 * * @param data 矩形框內nv21圖像數據 * @param width 圖像寬度 * @param height 圖像高度 * @return * @throws Exception */ @Override public Result scan(byte[] data, int width, int height) throws Exception { // 如果你想轉為Bitmap,請使用NV21.nv21ToBitmap(byte[] nv21, int width, int height) return null; } });
這里沒給默認的預覽頁面,需要自己自定義,下面給個例子
class ViewFinder extends View implements IViewFinder { private Rect framingRect;//掃碼框所占區域 private float widthRatio = 0.8f;//掃碼框寬度占view總寬度的比例 private float heightWidthRatio = 1f;//掃碼框的高寬比 private int leftOffset = -1;//掃碼框相對于左邊的偏移量,若為負值,則掃碼框會水平居中 private int topOffset = -1;//掃碼框相對于頂部的偏移量,若為負值,則掃碼框會豎直居中 private int laserColor = 0xff008577;// 掃描線顏色 private int maskColor = 0x60000000;// 陰影顏色 private int borderColor = 0xff008577;// 邊框顏色 private int borderStrokeWidth = 12;// 邊框寬度 private int borderLineLength = 72;// 邊框長度 private Paint laserPaint;// 掃描線 private Paint maskPaint;// 陰影遮蓋畫筆 private Paint borderPaint;// 邊框畫筆 private int position; public ViewFinder(Context context) { super(context); setWillNotDraw(false);//需要進行繪制 laserPaint = new Paint(); laserPaint.setColor(laserColor); laserPaint.setStyle(Paint.Style.FILL); maskPaint = new Paint(); maskPaint.setColor(maskColor); borderPaint = new Paint(); borderPaint.setColor(borderColor); borderPaint.setStyle(Paint.Style.STROKE); borderPaint.setStrokeWidth(borderStrokeWidth); borderPaint.setAntiAlias(true); } @Override protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld) { updateFramingRect(); } @Override public void onDraw(Canvas canvas) { if (getFramingRect() == null) { return; } drawViewFinderMask(canvas); drawViewFinderBorder(canvas); drawLaser(canvas); } private void drawLaser(Canvas canvas) { Rect framingRect = getFramingRect(); int top = framingRect.top + 10 + position; canvas.drawRect(framingRect.left + 10, top, framingRect.right - 10, top + 5, laserPaint); position = framingRect.bottom - framingRect.top - 25 < position ? 0 : position + 2; //區域刷新 postInvalidateDelayed(20, framingRect.left + 10, framingRect.top + 10, framingRect.right - 10, framingRect.bottom - 10); } /** * 繪制掃碼框四周的陰影遮罩 */ private void drawViewFinderMask(Canvas canvas) { int width = canvas.getWidth(); int height = canvas.getHeight(); Rect framingRect = getFramingRect(); canvas.drawRect(0, 0, width, framingRect.top, maskPaint);//掃碼框頂部陰影 canvas.drawRect(0, framingRect.top, framingRect.left, framingRect.bottom, maskPaint);//掃碼框左邊陰影 canvas.drawRect(framingRect.right, framingRect.top, width, framingRect.bottom, maskPaint);//掃碼框右邊陰影 canvas.drawRect(0, framingRect.bottom, width, height, maskPaint);//掃碼框底部陰影 } /** * 繪制掃碼框的邊框 */ private void drawViewFinderBorder(Canvas canvas) { Rect framingRect = getFramingRect(); // Top-left corner Path path = new Path(); path.moveTo(framingRect.left, framingRect.top + borderLineLength); path.lineTo(framingRect.left, framingRect.top); path.lineTo(framingRect.left + borderLineLength, framingRect.top); canvas.drawPath(path, borderPaint); // Top-right corner path.moveTo(framingRect.right, framingRect.top + borderLineLength); path.lineTo(framingRect.right, framingRect.top); path.lineTo(framingRect.right - borderLineLength, framingRect.top); canvas.drawPath(path, borderPaint); // Bottom-right corner path.moveTo(framingRect.right, framingRect.bottom - borderLineLength); path.lineTo(framingRect.right, framingRect.bottom); path.lineTo(framingRect.right - borderLineLength, framingRect.bottom); canvas.drawPath(path, borderPaint); // Bottom-left corner path.moveTo(framingRect.left, framingRect.bottom - borderLineLength); path.lineTo(framingRect.left, framingRect.bottom); path.lineTo(framingRect.left + borderLineLength, framingRect.bottom); canvas.drawPath(path, borderPaint); } /** * 設置framingRect的值(掃碼框所占的區域) */ private synchronized void updateFramingRect() { Point viewSize = new Point(getWidth(), getHeight()); int width, height; width = (int) (getWidth() * widthRatio); height = (int) (heightWidthRatio * width); int left, top; if (leftOffset < 0) { left = (viewSize.x - width) / 2;//水平居中 } else { left = leftOffset; } if (topOffset < 0) { top = (viewSize.y - height) / 2;//豎直居中 } else { top = topOffset; } framingRect = new Rect(left, top, left + width, top + height); } @Override public Rect getFramingRect() { return framingRect; } }
回調說明
方法說明
代碼混淆
-dontwarn com.shouzhong.** -keep class com.shouzhong.** {*;} -dontwarn com.google.zxing.** -keep class com.google.zxing.** -dontwarn net.sourceforge.zbar.** -keep class net.sourceforge.zbar.** {*;} -keep class com.wintone.bankcard.** {*;} -dontwarn com.wintone.bankcard.** -keep class exocr.exocrengine.** {*;} -dontwarn exocr.exocrengine.**
更多使用方法可以查看官方文檔
開源地址:
https://github.com/shouzhong/Scanner
更多更優質的資訊,請關注我,你的支持會鼓勵我不斷分享更多更好的優質文章。