gpt4 book ai didi

android - java.lang.RuntimeException : takePicture failed in lollipop and marshmallow 错误

转载 作者:太空狗 更新时间:2023-10-29 16:13:51 24 4
gpt4 key购买 nike

我正在尝试制作一个应用程序,它在单个 Activity Intent 上拍摄正面图像然后拍摄背面图像,我的前置摄像头代码与下面相同并且工作得很好我什至调用完成来破坏表面和内部 onSurfaceDestroy() 我调用下面给出的 initBackCam() 方法,它再次正常工作,直到 onSurfaceChanged() 内的 onSurfaceChanged() 我正在调用 pCamera.takePicture(null,mPictureCallback,mPictureCallback) 抛出异常 takePicture Failed

注意:这段代码工作正常,直到 kitkat 在 lollipop 和 marshmallow 中失败了

我尝试在前置和后置摄像头之间延迟,我尝试将后置摄像头的代码放在不同的 Activity 中,但它仍然会抛出相同的错误,我已经释放了摄像头并在拍照前调用了 startPreview() 但它仍然抛出相同的错误错误

我使用了以下权限

<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />
<uses-feature android:name="android.hardware.camera.back" android:required="false" />

请帮忙!!!!

public static Bitmap bitmap;
public SurfaceHolder pSurfaceHolder;
private Camera pCamera;
private boolean pPreviewRunning = false;
private RelativeLayout pCameraPreview;
private BackCameraService backCam;

private void initBackCamera() {
pCameraPreview = (RelativeLayout) findViewById(R.id.preview_back_camera);
backCam = new BackCameraService(getBaseContext(), pCamera);
pCameraPreview.addView(backCam);
}

public class BackCameraService extends SurfaceView implements SurfaceHolder.Callback {
public Camera pCamera;
private Context pContext;

Camera.PictureCallback mBackPictureCallback = new Camera.PictureCallback() {

public void onPictureTaken(byte[] data, Camera camera) {
pCamera = camera;
if (data != null) {
pCamera.stopPreview();
pPreviewRunning = false;
pCamera.release();

try {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inScaled = false;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
Matrix matrix = new Matrix();
matrix.postRotate(-90);
File(Environment.getExternalStorageDirectory(), "MyImage");
if (!(root.isDirectory() && root.exists())) root.mkdirs();
File backImg = new File(root.getAbsolutePath(), "backImg");
if (!(backImg.isDirectory() && backImg.exists())) backImg.mkdirs();
File file = new File(backImg.getAbsolutePath(), "backImage.jpg");
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 75, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}

} catch (Exception e) {
e.printStackTrace();
}
setResult(585);
}
}
};

BackCameraService(Context context, Camera camera) {
super(context);
Log.d("jagteraho", "backCam started");
pContext = context;
pCamera = camera;
pSurfaceHolder = getHolder();
pSurfaceHolder.addCallback(BackCameraService.this);
pSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceCreated(pSurfaceHolder);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.d("jagteraho", "backCam surface created");
pCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
surfaceChanged(holder, 2, 2, 2);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Log.d("jagteraho", "backCam surface changed");
if (pPreviewRunning) {
pCamera.stopPreview();
}
Camera.Parameters p = pCamera.getParameters();
List<Camera.Size> previewSizes = p.getSupportedPreviewSizes();
Camera.Size previewSize = previewSizes.get(1);
p.setPreviewSize(previewSize.width, previewSize.height);
try {
pCamera.setParameters(p);
pCamera.setPreviewDisplay(holder);
} catch (Exception e) {
e.printStackTrace();
}
pCamera.startPreview();
pPreviewRunning = true;
pCamera.takePicture(null, mBackPictureCallback, mBackPictureCallback);
surfaceDestroyed(holder);
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d("jagteraho", "backCam surface destroyed");
}
}

Log如下:

02-15 19:18:11.159 6382-6382  surface created
02-15 19:18:11.161 206-892/? I/CameraService﹕ CameraService::connect call (PID 6382 , camera ID 1) for HAL version default and Camera API version 1
02-15 19:18:11.222 206-892/? I/Camera2ClientBase﹕ Camera 1: Opened. Client: (PID 6382, UID 10037)
02-15 19:18:11.485 6382-6382 surface changed
02-15 19:18:11.553 779-800/? I/ActivityManager﹕ Displayed +473ms
02-15 19:18:13.849 206-818/? I/Camera2ClientBase﹕ Closed Camera 1. Client was: (PID 6382, UID 10037)
02-15 19:18:14.102 6382-6382 ﹕ surface destroyed
02-15 19:18:14.103 6382-6382 ﹕ backCam started
02-15 19:18:14.103 6382-6382 ﹕ backCam surface created
02-15 19:18:14.103 206-1764/? I/CameraService﹕ CameraService::connect call (PID 6382 , camera ID 0) for HAL version default and Camera API version 1
02-15 19:18:14.165 206-1764/? I/Camera2ClientBase﹕ Camera 0: Opened. Client:(PID 6382, UID 10037)
02-15 19:18:14.238 6382-6382 ﹕ backCam surface changed
02-15 19:18:14.240 6382-6382 ﹕ app passed NULL surface
02-15 19:18:14.241 6382-6382 D/AndroidRuntime﹕ Shutting down VM
02-15 19:18:14.243 6382-6382 E/CustomActivityOnCrash﹕ App has crashed, executing CustomActivityOnCrash's UncaughtExceptionHandler
java.lang.RuntimeException: takePicture failed
at android.hardware.Camera.native_takePicture(Native Method)
at android.hardware.Camera.takePicture(Camera.java:1434)
at android.hardware.Camera.takePicture(Camera.java:1379)
at com.aspeage.jagteraho.AlarmActivity$BackCameraService.surfaceChanged(AlarmActivity.java:922)
at com.aspeage.jagteraho.AlarmActivity$BackCameraService.surfaceCreated(AlarmActivity.java:872)
at com.aspeage.jagteraho.AlarmActivity$BackCameraService.<init>(AlarmActivity.java:865)
at com.aspeage.jagteraho.AlarmActivity.initBackCamera(AlarmActivity.java:788)
at com.aspeage.jagteraho.AlarmActivity.access$400(AlarmActivity.java:77)
at com.aspeage.jagteraho.AlarmActivity$FrontCameraService.surfaceDestroyed(AlarmActivity.java:782)
at android.view.SurfaceView.updateWindow(SurfaceView.java:567)
at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:239)
at android.view.View.dispatchWindowVisibilityChanged(View.java:9647)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1309)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1309)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1309)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1309)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1309)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1383)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

最佳答案

试试这个

添加:

SurfaceTexture st = new SurfaceTexture(MODE_PRIVATE); 
pCamera.setPreviewTexture(st);

之前:

pCamera.startPreview();

takePicture failed

希望对你有帮助

关于android - java.lang.RuntimeException : takePicture failed in lollipop and marshmallow 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35412664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com