我正在使用 Zbar 阅读 QRCode。我用这个 https://github.com/DushyanthMaguluru/ZBarScanner我的 Activity 的例子。 问题是如何在我的 FrameLayout 上显示 cameraView?
编辑:
@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();
。您不想这样做,因为 CameraPreview
会为您完成。
我不会使用 FrameLayout,因为项目是一个接一个地放置的,而您想最后放置 cameraPreview。所以你应该在另一个 RelativeLayout
中有一个 LinearLayout
(我知道,它效率不高,但现在没问题)。类似于(您的 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" />
关于java - 如何为android ZBar QrCode Reader自定义CameraView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18079439/