我正在使用Zbar阅读QRCode。我将此https://github.com/DushyanthMaguluru/ZBarScanner示例用于我的活动。 问题是如何在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); }
答案 0 :(得分: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" />