Recently, the project's barcode scan needs to be changed to a horizontal screen. I searched it online, and then modified the following code to scan the horizontal screen barcode.
Before modification:
Modify the Code as follows:
1. Modify the activity configuration file
<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" is the key default value: Landscape
2. Modify getframingrectinpreview () of cameramanager. Java ():
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;
3. Modify cameraconfigurationmanagSetdesiredcameraparamete in ER. JavaRS function,
Add setdisplayorientation (camera,
180); In setdesiredcameraparameteAdded outside RS Function
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. Modify the decode function in decodehandler. java,
Set planaryuvluminancesourceSource
= 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)
5. This problem has been solved, but there is only one problem: Compatibility
If the zxing version is 1.7, for the previous version, when the mobile phone scans, the possible points depicted by the mobile phone are different from the actual ones, so you need to modify viewfinderview. java ondraw function, in collection <resultpoint>
Currentpossible = possibleresultpoints; added before
Rect previewFrame = CameraManager.get().getFramingRectInPreview(); float scaleX = frame.width() / (float) previewFrame.width(); float scaleY = frame.height() / (float) previewFrame.height();
Use the canvas in the two for statements below this function. drawcircle (frame. left + point. getx (), frame. top + point. gety (), 6.0f, paint); change to canvas. drawcircle (frame. left + (INT) (Point. getx () * Scalex ), Frame. Top + (INT) (Point. Gety () * Scaley ), 3.0f, Paint );Note: It is a for statement that modifies two locations. If you want to enlarge the scan box, modify cameramanager. the getframingrect function in Java modifies framingrect = new rect (leftoffset, topoffset, leftoffset + width, topoffset + height) to framingrect = new rect (0, topoffset, leftoffset * 2 + width, topoffset + height); this modification will flatten the left and right sides of the scan box with the screen. However, the black border does not seem quite recognizable to the user, so I modified the border color and changed the viewfinderview. paint. setcolor (framecolor); change to paint. setcolor (lasercolor);, Which is changed to the same color as the scan center.
The modification is as follows: