How to set up CameraView for Android ZBar QrCode Reader

I use Zbar to read QRCode. I use this https://github.com/DushyanthMaguluru/ZBarScanner example for my activity. Question: how can I show CameraView on my FrameLayout?

EDIT:

  @Override
     public void onCreate (Bundle savedInstanceState)
     {
         super.onCreate (savedInstanceState);

setContentView(R.layout.main); //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); mCamera = getCameraInstance(); if(!isCameraAvailable()) { cancelRequest(); return; } requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); mAutoFocusHandler = new Handler(); setupScanner(); mPreview = new CameraPreview(this, this, autoFocusCB); //setContentView(mPreview); FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview); preview.addView(mPreview); } 

+6
source share
1 answer

First of all, delete the line that seems accessible to the camera for me: mCamera = getCameraInstance(); . You do not want to do this, because CameraPreview will do it for you.

I would not use FrameLayout, because the elements are put one after another, and you want to finally put your camera. So you should have LinearLayout in another RelativeLayout (I know it is inefficient, but so far fine). Something like (your main.xml layout):

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/zbar_layout_area" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout> <ImageView android:id="@+id/my_own_image_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /> <TextView android:id="@+id/my_own_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" /> </RelativeLayout> 

Now you need to put CameraPreview in zbar_layout_area . For this reason, try changing the code to (blind coding):

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (!isCameraAvailable()) { // Cancel request if there is no rear-facing camera. cancelRequest(); return; } // Hide the window title. requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); mAutoFocusHandler = new Handler(); // Create and configure the ImageScanner; setupScanner(); // Create a RelativeLayout container that will hold a SurfaceView, // and set it as the content of our activity. mPreview = new CameraPreview(this, this, autoFocusCB); LinearLayout zbarLayout = (LinearLayout) findViewById(R.id.zbar_layout_area); mPreview.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); zbarLayout.addView(mPreview); } 

Also make sure that you have installed enough patches:

 <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" android:required="false" /> <uses-feature android:name="android.hardware.camera.autofocus" android:required="false" /> 
+4
source

Source: https://habr.com/ru/post/951120/


All Articles