基於Zxing的二維碼的二維碼掃描之橫屏掃描
阿新 • • 發佈:2018-06-04
ews name pla eight 網上 HA 如果 log 簡單的
最近項目條碼掃描要改為橫屏,網上所搜了一下,然後發現我寫的需要改動幾行代碼就可以了,還是很給力的。
如未查看之前的代碼,請移步:
基於Zxing的二維碼生成和二維碼掃描
修改下面寫代碼就可以實現橫屏條碼掃描了
具體代碼修改如下:
1、修改 activity配置文件
查看文本打印?
- <activity
- android:name=".CaptureActivity"
- android:configChanges="orientation|keyboardHidden"
- android:label="@string/app_name"
- android:screenOrientation="portrait"
- android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
- android:windowSoftInputMode="stateAlwaysHidden" >
- </activity>
android:screenOrientation="portrait" 是關鍵 默認是landscape
2、修改CameraConfigurationManager.java中的setDesiredCameraParameters函數,
在 camera.setDisplayOrientation(90);
改成 camera.setDisplayOrientation(0);
3、修改CameraManager.java的getFramingRectInPreview()函數中修改成:
[java] view plain copy
- rect.left = rect.left * cameraResolution.x / screenResolution.x;
- rect.right = rect.right * cameraResolution.x /
- screenResolution.x;
- rect.top = rect.top * cameraResolution.y / screenResolution.y;
- rect.bottom = rect.bottom * cameraResolution.y /
- screenResolution.y;
4、修改DecodeHandler.java中的decode函數,
[java] view plain copy
- // byte[] rotatedData = new byte[data.length];
- // for (int y = 0; y < height; y++) {
- // for (int x = 0; x < width; x++)
- // rotatedData[x * height + height - y - 1] = data[x + y * width];
- // }
- // int tmp = width; // Here we are swapping, that‘s the difference to #11
- // width = height;
- // height = tmp;
- //
- PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(data, width, height);
然後修改函數中的變量,將rotatedData修改成data;
這樣就能夠實現橫屏掃描了。
如果還需要改動掃描框的大小需要去CameraManager類裏面改動:
[java] view plain copy
- //改變掃描框的最大、最小長寬
- private static final int MIN_FRAME_WIDTH = 240;
- private static final int MIN_FRAME_HEIGHT = 240;
- private static final int MAX_FRAME_WIDTH = 720;
- private static final int MAX_FRAME_HEIGHT = 480;
好了,這樣就可以很簡單的是實現橫屏掃描了。
基於Zxing的二維碼的二維碼掃描之橫屏掃描