Android Zxing框架掃描解決掃描框大小,圖片壓縮問題
阿新 • • 發佈:2019-01-26
在我們是Zxing框架進行二維碼掃描的時候,會發現,現在手機隨著解析度的增加,那個掃描框會越來越小,在1920*1280和1280*720還算比較正常,但是三星的幾款手機note4,5,S6,等幾款手機解析度高達2560*1440,甚至一些手機高達3660的吧記不清了。
在這些手機掃描的時候,彈出的掃描Activity中間SurfaceView掃描視窗小的也是醉了,
不說了解決辦法:
1:找到啟動掃描的Intent 即:
Intent openCameraIntent = new Intent(context,CaptureActivity.class);
startActivityForResult(openCameraIntent,0);
2:跟進CaptureActivity類,在類中搜索:CameraManager,跟進進去CameraManager類中:最上面四行引數就是設定寬高的,
其中後邊我進行了dp轉換,這樣每個解析度的螢幕掃描視窗大小都是固定的。完美解決問題。private static final int MIN_FRAME_WIDTH = (int) DP_SP_PX_Utils.dp2px(MyApplication.instance.getResources(),180); private static final int MIN_FRAME_HEIGHT = (int) DP_SP_PX_Utils.dp2px(MyApplication.instance.getResources(),180); private static final int MAX_FRAME_WIDTH = (int) DP_SP_PX_Utils.dp2px(MyApplication.instance.getResources(),240); private static final int MAX_FRAME_HEIGHT = (int) DP_SP_PX_Utils.dp2px(MyApplication.instance.getResources(),240);
第二個問題:每次掃描後圖片都會壓縮下
解決方法:在Zxing包下的camera包下找到CameraConfigurationManager.java類,修改:
搜尋initFromCameraParameters 這個方法,在該方法下找到 Log.d(TAG, "Screen resolution: " + screenResolution); 這句話,在這句話下面新增這些程式碼:
Point screenResolutionForCamera = new Point(); screenResolutionForCamera.x = screenResolution.x; screenResolutionForCamera.y = screenResolution.y; // preview size is always something like 480*320, other 320*480 if (screenResolution.x < screenResolution.y) { screenResolutionForCamera.x = screenResolution.y; screenResolutionForCamera.y = screenResolution.x; }
然後下面有一行這樣的程式碼:
- cameraResolution = getCameraResolution(parameters, screenResolution);
中的screenResolution改為 screenResolutionForCamera
如下:
- cameraResolution = getCameraResolution(parameters, screenResoluti
儲存,執行完美解決,在此附上二維碼掃描和二維碼生成demo 不需要積分,咱們互相學習,共同跟進步,有問題可以在評論區提出。