本文介绍了如何自定义CameraView为Android ZBar Qr的code阅读器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Zbar读取QR code。我用这个 https://github.com/DushyanthMaguluru/ZBarScanner 例如我的活动。现在的问题是我怎么能显示cameraView在我的FrameLayout?

I am using Zbar for reading QRCode. I use this https://github.com/DushyanthMaguluru/ZBarScanner example for my activity. The Question is how can I show cameraView on my FrameLayout ?

编辑:   


        @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);
}

推荐答案

首先,除去在我看来,收购摄像机接入该行: mCamera = getCameraInstance(); 。你不想这样做,因为相机preVIEW 会为你做它。

First of all remove the line that seems to me to acquire camera access: mCamera = getCameraInstance();. You don't want to do that as the CameraPreview will do it for you.

我不会用的FrameLayout的物品放在一个接一个,你想要把你的相机preVIEW最后。所以,你应该有一个的LinearLayout 在另一 RelativeLayout的(我知道,这不是有效的,但是这很好现在)。喜欢的东西(你的main.xml布局):

I wouldn't use a FrameLayout as items are put one after the other and you want to put your cameraPreview at last. So you should have a LinearLayout in another RelativeLayout (I know, it's not efficient, but that's fine for now). 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>

现在,你需要把相机preVIEW在 zbar_layout_area 。出于这个原因,试图改变code到(盲编码):

Now, you would need to put the CameraPreview in zbar_layout_area. For this reason, try to change 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);
}

另外,还要确保您已设置足够的prermissions:

Also make sure you have set enough prermissions:

<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" />

这篇关于如何自定义CameraView为Android ZBar Qr的code阅读器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 04:46