Get access to Zxing QR scanner camera view

2019-08-13 20:35发布

问题:

I am new to android development and I am working on an android project where I have to integrate QR scanner. So I thought I would integrate Zxing QR scanner and came across this library https://github.com/dm77/barcodescanner, Following the instructions provided I have successfully integrated and scanned a QR as well. Now I want to customise the camera view. The problem is there is no documentation on how to get access to the camera layout. Since I am new I might be missing something.

I have read through many Zxing related threads but I din find any solution.

Any help will be greatly appreciated either by letting me know on how I can get access to the camera view or pointing me out to some articles. Once I know how then I can complete the rest.

I would be happy to provide any further information.

Thanks in advance.

Update:

public class QrScanActivity extends BaseActivity implements ZXingScannerView.ResultHandler {
    private ZXingScannerView mScannerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_scan);

        // Programmatically initialize the scanner view
        mScannerView = new ZXingScannerView(this);
        // Set the scanner view as the content view
        setContentView(mScannerView);
    }

mScannerView is the view from the library. I want to get access to that view. I have a view called as activity_my_scan. I can add custom layout to that and use that but I don't know how to bypass the layout being used by the library.

回答1:

Instead of just adding this lib as a jar using gradle's

compile 'me.dm7.barcodescanner:zxing:1.6.3'

you could clone the project from github or download a zip and uzip it and integrate to your project as a lib project. And then make any desired changes in its sources and its layouts.



回答2:

ZXingScannerView extends BarcodeScannerView. You can access this by going to the declaration of ZXingScannerView (in Android Studio Ctrl+B).

public class ZXingScannerView extends BarcodeScannerView {
private MultiFormatReader mMultiFormatReader;
public static final List<BarcodeFormat> ALL_FORMATS = new ArrayList();
private List<BarcodeFormat> mFormats;
private ZXingScannerView.ResultHandler mResultHandler;

If you go the declaration of BarcodeScannerView, you'll notice a method called setupLayout() which formats the layout:

public void setupLayout() {
    this.mPreview = new CameraPreview(this.getContext());
    this.mViewFinderView = new ViewFinderView(this.getContext());
    RelativeLayout relativeLayout = new RelativeLayout(this.getContext());
    relativeLayout.setGravity(17);
    relativeLayout.setBackgroundColor(-16777216);
    relativeLayout.addView(this.mPreview);
    this.addView(relativeLayout);
    this.addView(this.mViewFinderView);
}

By going to the declaration of "CameraPreview" you'll be able to get more info for how the camera is laid out and you could maybe extend ZXingScannerView to edit the layout.

Hope this helps!

Cheers!



回答3:

Well in your case i would try to include the layout into another layout, maybe it helps:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:background="@color/app_bg"
    android:gravity="center_horizontal">

    <include layout="@layout/titlebar"/>

    <TextView android:layout_width=”match_parent”
              android:layout_height="wrap_content"
              android:text="@string/hello"
              android:padding="10dp" />

    ...

</LinearLayout>

And of course there is information about that here ;)