最近项目条码扫描要改为横屏,网上所搜了一下,然后修改下面写代码就可以实现横屏条码扫描了
修改前效果图如下
具体代码修改如下:
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修改CameraManager.java的getFramingRectInPreview():
rect.left = rect.left * cameraResolution.y / screenResolution.x; rect.right = rect.right * cameraResolution.y / screenResolution.x; rect.top = rect.top * cameraResolution.x / screenResolution.y; rect.bottom = rect.bottom * cameraResolution.x / screenResolution.y;
在setZoom(parameters);后增加setDisplayOrientation(camera, 180); 在setDesiredCameraParameters函数外增加
private void setDisplayOrientation(Camera camera, int angle) { Method downPolymorphic; try { downPolymorphic = camera.getClass().getMethod( “setDisplayOrientation”, new Class[] { int.class }); if (downPolymorphic != null) downPolymorphic.invoke(camera, new Object[] { angle }); } catch (Exception e1) { } }4 修改DecodeHandler.java中的decode函数,
将PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(data, width, height);修改为
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(rotatedData, width, height)
如果是ZXing的版本是1.7的,对于之前的版本来讲当手机扫描时,手机描绘的可能点与实际有偏差,所以需要修改ViewfinderView.java的onDraw函数,在Collection<ResultPoint> currentPossible = possibleResultPoints;前增加
Rect previewFrame = CameraManager.get().getFramingRectInPreview(); float scaleX = frame.width() / (float) previewFrame.width(); float scaleY = frame.height() / (float) previewFrame.height();