最近项目条码扫描要改为横屏,网上所搜了一下,然后发现我写的需要改动几行代码就可以了,还是很给力的。
如未查看之前的代码,请移步:
修改下面写代码就可以实现横屏条码扫描了
具体代码修改如下:
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()函数中修改成:
- 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函数,
- // 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类里面改动:
- //改变扫描框的最大、最小长宽
- 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;
好了,这样就可以很简单的是实现横屏扫描了。