How to use Zxing in portrait mode?

Currently, zxing is only supported in landscape mode. For my application I need to use in portrait mode

+10
android
Nov 04 2018-11-11T00:
source share
5 answers

Just review the question for using Zxing in portrait mode.

+17
Nov 04 2018-11-11T00:
source share
— -

setDisplayOrientation(int) does not affect the byte array order passed to PreviewCallback.onPreviewFrame . (see JavaDoc for more information)

This means you need to rotate the data return from previewCallback, but this is yuv data, you need to convert it to rgb data, and then rotate it. you can scan the barcode in portrait mode, but it will take longer because it takes longer to process the data from yuv to rgb and rotate the rgb data. so the question here is how can we get yuv data from previewcallback for portrait mode? when we set setPreviewSize for the camera parameters, it will get an exception if the preview is not supported. That is the question of this problem. If the camera driver does not support the preview mode for portrait mode (height> width), you will not be able to get yuv data in portrait mode. And everything else is up to you, you can scan the barcode in portrait mode, but it takes more time or you need to change the screen orientation to landscape to get the result faster.

+5
Mar 01 2018-12-12T00:
source share

Here is the solution for Portrait mode Scanning

first declare these two lines at your application level gradle file

 compile 'com.journeyapps:zxing-android-embedded:3.0.1@aar' compile 'com.google.zxing:core:3.2.0' 

Define the button in your xml file and in the Onclick Listener of the button write the code below in the Java MainActivity file

 IntentIntegrator integrator = new IntentIntegrator(this); integrator.setPrompt("Scan a barcode"); integrator.setCameraId(0); // Use a specific camera of the device integrator.setOrientationLocked(true); integrator.setBeepEnabled(true); integrator.setCaptureActivity(CaptureActivityPortrait.class); integrator.initiateScan(); 

Enter the code below into your Java MainActivity file after the onCreate () method

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); if(result != null) { if(result.getContents() == null) { Log.d("MainActivity", "Cancelled scan"); Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show(); } else { Log.d("MainActivity", "Scanned"); st_scanned_result = result.getContents(); Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show(); } } } 

then create a class called CaptureActivityPortrait that extends CaptureActivity. Class looks lower

  package soAndSo(Your PackageName); import com.journeyapps.barcodescanner.CaptureActivity; public class CaptureActivityPortrait extends CaptureActivity { } 

And the most important thing is to declare your CaptureActivityPortrait in the manifest file, as shown below

 <activity android:name=".CaptureActivityPortrait" android:screenOrientation="portrait" android:stateNotNeeded="true" android:theme="@style/zxing_CaptureTheme" android:windowSoftInputMode="stateAlwaysHidden"></activity> 
+5
Apr 07 '17 at 10:58 on
source share
  • To make the screen work in portrait orientation, set the portrait orientation for the action (for example, in the manifest), and then configure the camera: use the camera.setDisplayOrientation (90) function in CameraConfigurationManager.setDesiredCameraParameters (Camera). But keep in mind that:

    • setDisplayOrientation (int) requires Android 2.2
    • setDisplayOrientation (int) does not affect the order of the byte array passed to PreviewCallback.onPreviewFrame. (See JavaDoc for more information.)
  • Since the preview frames are always in the "landscape", we need to rotate them. I used a clockwise rotation, suggested comment # 11 . Remember to change the width and height settings after rotation. DecodeHandler.java, rotate data before buildLuminanceSource in decode (byte [] data, int width, int height)

     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 the difference to #11 width = height; height = tmp; 
  • I also changed CameraManager.java, getFramingRectInPreview () as recommended by # c11:

     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; 
  • I did not change getCameraResolution (). This is the second difference from # c11.

As a result, I have UPC and other scans of 1D codes in portrait.

PS You can also adjust the size of the FramingRect (which is the rectangle visible on the screen during the scan), and the FramingRectInPreview will be automatically adjusted.

+2
Mar 19 '13 at 10:58 on
source share

Use this Android library https://github.com/SudarAbisheck/ZXing-Orient

It supports both portrait and landscape orientations.

+1
Mar 01 '16 at 4:57
source share



All Articles