6

我正在使用 Zbar 来阅读 QRCode。我将这个https://github.com/DushyanthMaguluru/ZBarScanner示例用于我的活动。问题是如何在我的 FrameLayout 上显示 cameraView ?

编辑:

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

4

1 回答 1

4

首先删除在我看来获取相机访问权限的行:mCamera = getCameraInstance();. 你不想这样做,因为CameraPreview它会为你做。

我不会使用 FrameLayout,因为项目一个接一个地放置,并且您希望最后放置 cameraPreview。所以你应该有LinearLayout另一个RelativeLayout(我知道,它效率不高,但现在没关系)。类似于(您的 main.xml 布局):

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

现在,您需要将 CameraPreview 放入zbar_layout_area. 为此,尝试将代码更改为(盲编码):

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

还要确保您设置了足够的权限:

<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" />
于 2013-08-06T12:43:53.143 回答