在我們是Zxing框架進行二維碼掃描的時候,會發現,現在手機隨着分辨率的增加,那個掃描框會越來越小解決辦法:
1:找到CameraManager類,最上面四行參數就是設置寬高的,
private static final int MIN_FRAME_WIDTH = (int) DensityUtils.dp2px(MyApplication.getInstance(),180);
private static final int MIN_FRAME_HEIGHT = (int) DensityUtils.dp2px(MyApplication.getInstance(),180);
private static final int MAX_FRAME_WIDTH = (int) DensityUtils.dp2px(MyApplication.getInstance(),240);
private static final int MAX_FRAME_HEIGHT = (int) DensityUtils.dp2px(MyApplication.getInstance(),240);
第二個問題:每次掃描後圖片都會壓縮下
解決方法:在Zxing包下的camera包下找到CameraConfigurationManager.java類,修改:
搜索initFromCameraParameters 這個方法,在該方法下找到 Log.d(TAG, "Screen resolution: " + screenResolution); 這句話,在這句話下面添加這些代碼:
Point screenResolutionForCamera = new Point();
screenResolutionForCamera.x = screenResolution.x;
screenResolutionForCamera.y = screenResolution.y;
if (screenResolution.x < screenResolution.y) {
screenResolutionForCamera.x = screenResolution.y;
screenResolutionForCamera.y = screenResolution.x;
}
然後下面有一行這樣的代碼:
- cameraResolution = getCameraResolution(parameters, screenResolution);
中的screenResolution改爲 screenResolutionForCamera
第三個問題:調整掃描區域 優化取圖速度
Zxing 是google提供的二維碼掃描工程
Demo本身默認的掃圖區域最大隻有 360*480 需要拉開很遠的距離才能將整個二維碼掃描到
因此需要我們自己調整取圖大小
在CameraManager.java這個類中進行調整
默認的大小是 以下這4個參數
- // private static final int MIN_FRAME_WIDTH = 240;
- // private static final int MIN_FRAME_HEIGHT = 240;
- // private static final int MAX_FRAME_WIDTH = 480;
- // private static final int MAX_FRAME_HEIGHT = 360;
參數實際在 getFramingRect() 方法中起作用
以下是原本Demo中提供的
- /**
- * Calculates the framing rect which the UI should draw to show the user where to place the
- * barcode. This target helps with alignment as well as forces the user to hold the device
- * far enough away to ensure the image will be in focus.
- *
- * @return The rectangle to draw on screen in window coordinates.
- */
- public Rect getFramingRect() {
- Point screenResolution = configManager.getScreenResolution();
- if (framingRect == null) {
- if (camera == null) {
- return null;
- }
- //原生
- int width = screenResolution.x * 3 / 4;
- if (width < MIN_FRAME_WIDTH) {
- width = MIN_FRAME_WIDTH;
- } else if (width > MAX_FRAME_WIDTH) {
- width = MAX_FRAME_WIDTH;
- }
- int height = screenResolution.y * 3 / 4;
- if (height < MIN_FRAME_HEIGHT) {
- height = MIN_FRAME_HEIGHT;
- } else if (height > MAX_FRAME_HEIGHT) {
- height = MAX_FRAME_HEIGHT;
- }
- int leftOffset = (screenResolution.x - width) / 2;
- int topOffset = (screenResolution.y - height) / 2;
- framingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height);
- Log.d(TAG, "Calculated framing rect: " + framingRect);
- }
- return framingRect;
- }
我將代碼改成了
- public Rect getFramingRect() {
- Point screenResolution = configManager.getScreenResolution();
- if (framingRect == null) {
- if (camera == null) {
- return null;
- }
- //修改之後
- int width = screenResolution.x * 3 / 4;
- int leftOffset = (screenResolution.x - width) / 2;
- int topOffset = (screenResolution.y - width ) / 3;
- framingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + width );
- Log.d(TAG, "Calculated framing rect: " + framingRect);
- }
- return framingRect;
- }
當然...取圖改的這麼大 會多佔一點內存....相應的掃描的時候快得多