"错误1 QUOT;在Android的摄像头覆盖期间发生 [英] "Error 1" occurring during camera overlay in Android
问题描述
我创建了一个具有SurfaceView和SurfaceHolder以创造一个摄像头覆盖的活动。出于某种原因,我得到亚行以下内容: 10月4日至8日:54:06.747:E /摄像机(1152):错误1 即可。我可以拍照,但我不能让任何类型的preVIEW的。这里是code我使用的:
I have created an Activity that has a SurfaceView and a SurfaceHolder in order to create a Camera overlay. For some reason, I am getting the following in ADB: 04-08 10:54:06.747: E/Camera(1152): Error 1. I am able to take a picture, but I cannot get any kind of preview. Here is the code I am using:
public class MyActivity extends Activity implements
SurfaceHolder.Callback, Camera.ShutterCallback, Camera.PictureCallback, OnClickListener {
private Camera mCamera;
private SurfaceView videoPreview;
private SurfaceHolder surfaceHolder;
private Button cancel;
private Button takePicture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.camera_layout);
videoPreview = (SurfaceView) findViewById(R.id.video_preview);
cancel = (Button) findViewById(R.id.cancel);
cancel.setOnClickListener(this);
takePicture = (Button) findViewById(R.id.take_picture);
takePicture.setOnClickListener(this);
surfaceHolder = videoPreview.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// Camera.open(getNumberOfCameras() - 1); => access front facing camera as well
mCamera = Camera.open();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Camera.Parameters parameters = mCamera.getParameters();
// pick the device's largest supported camera size
List<Camera.Size> cameraSizes = parameters.getSupportedPreviewSizes();
// the first size is the largest
Camera.Size selectedSize = cameraSizes.get(0);
parameters.setPreviewSize(selectedSize.width, selectedSize.height);
mCamera.setDisplayOrientation(90);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(surfaceHolder);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
protected void onPause() {
super.onPause();
mCamera.stopPreview();
}
@Override
protected void onDestroy() {
super.onDestroy();
mCamera.release();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.cancel:
finish();
break;
case R.id.take_picture:
mCamera.takePicture(this, null, null, this);
break;
}
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
// store the picture
FileOutputStream fos = null;
File imageFile = null;
try {
imageFile = new File(Environment.getExternalStorageDirectory(), "custom_image1.png");
fos = new FileOutputStream(imageFile);
fos.write(data);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
mCamera.startPreview();
}
@Override
public void onShutter() {
// perform an animation
}
}
和这里的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<SurfaceView
android:layout_weight="1"
android:id="@+id/video_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/cancel"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="Cancel" />
<Button
android:id="@+id/take_picture"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Snap Photo" />
</LinearLayout>
我试图将此更改为RelativeLayout的,但无济于事。我真的觉得我刚才错过了简单的东西。任何帮助是极大的AP preciated。谢谢!
I have tried changing this to RelativeLayout but to no avail. I really think I have just missed something simple. Any help is greatly appreciated. Thanks!
推荐答案
您没有onResume()方法来调用再次启动preVIEW(),因此,当您拍摄照片时,preVIEW没有运行,因此捕获失败。
You have no onResume() method to call startPreview() again, hence when you take a picture, the preview is not running, so the capture fails.
要测试这一点,尝试添加camera.start preVIEW()可以捕获右之前的图像,并看看是否能工程。
To test this, try adding camera.startPreview() right before you capture an image, and see if that works.
这篇关于&QUOT;错误1 QUOT;在Android的摄像头覆盖期间发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!