gpt4 book ai didi

android - Camera onPictureTaken() 有时不被调用

转载 作者:行者123 更新时间:2023-11-29 17:48:41 27 4
gpt4 key购买 nike

我有一个发布到 Google Play 的应用程序,它有时会使用来自后台服务的前置摄像头拍照。现在,我收到用户的投诉,说应用程序的这个特定部分不是很一致,我知道这是真的,因为它通常在我调用 camera.takePicture() 后停止。 . - 即 PictureCallback不被调用。

关于此问题,SO 上还有其他几个问题,herehere ,我已经浏览了所有这些,他们的修复对我没有任何帮助。我的情况与其他人不同,因为在我的应用程序中,通常会调用 onPictureTaken,但有时会失败(大约 1/10)。

我在这个问题上摸索了 2 天,没有任何进展。

除其他事项外,我还尝试注释掉我在 PictureCapllback 中启动的线程,这似乎使它更加一致,但并不完全一致,因此它可能是某种内存问题。

    ...
// Take photo
takePhoto(new MyPictureCallback());
}

private class MyPictureCallback implements Camera.PictureCallback{
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.d(Constants.TAG_ALERT_PROCESS, "onPictureTaken() called");

// Release camera
cameraHandler.release();

// Continue process
new Thread(new GotPictureRunnable(data)).start();
}
}

public void takePhoto(Camera.PictureCallback callback) {

// Create new cameraInfo object
cameraInfo = new Camera.CameraInfo();

// Get camera object
camera = getCamera();

// If camera is null bail
if(camera == null){
return;
}

// Set camera options
setCameraParameters();
Log.v(Constants.TAG_ALERT_PROCESS, "Camera parameters set");

try{
camera.setPreviewTexture(new SurfaceTexture(0)); // Hack for no preview
Log.v(Constants.TAG_ALERT_PROCESS, "Preview set");
} catch (Exception e){
Log.v(Constants.TAG_ALERT_PROCESS, "Failed to set Preview Texture at camera.takePicture()");
e.printStackTrace();
return;
}

// Start the preview
camera.startPreview();
Log.v(Constants.TAG_ALERT_PROCESS, "Preview started");

// Preview needs time to start
Utilities.sleepThread(1000);
Log.v(Constants.TAG_ALERT_PROCESS, "Thread slept for 1000ms");

// Take picture and pass our callback
Log.v(Constants.TAG_ALERT_PROCESS, "Calling takePicture()");
camera.takePicture(null, null, callback);
}

private void setCameraParameters() {

// Create parameters object
Camera.Parameters params = this.camera.getParameters();

// Set picture size
setPictureSize(params, context);

// Set jpeg quality
params.setJpegQuality(getPictureQuality());

// Set orientation
params.setRotation(getRotation());

// Set Parameters
this.camera.setParameters(params);
}

public void release(){
if(camera != null){
camera.release();
camera = null;
}
}

登录失败

07-06 18:09:38.134     135-1051/? I/CameraClient﹕ Opening camera 1
07-06 18:09:38.134 135-1051/? I/CameraHAL﹕ camera_device open
07-06 18:09:38.149 16009-19806/com...V/myTag﹕ Got front Camera
07-06 18:09:38.157 16009-19806/com...V/myTag﹕ Set camera parameters
07-06 18:09:38.157 16009-19806/com...V/myTag﹕ Preview set
07-06 18:09:38.540 16009-19806/com...V/myTag﹕ Preview started
07-06 18:09:38.548 135-19815/? D/﹕ PPM: Standby to first shot: Sensor Change completed - :390.137 ms : 1404662978557 ms
07-06 18:09:39.540 16009-19806/com...V/myTag﹕ Thread slept for 1000ms
07-06 18:09:39.540 16009-19806/com...V/myTag﹕ Calling takePicture()
07-06 18:09:39.774 135-19815/? D/﹕ PPM: Shot to snapshot: :228.852 ms : 1404662979777 ms
07-06 18:09:39.813 135-19815/? D/﹕ PPM: Shot to Jpeg: :274.231 ms : 1404662979823 ms
07-06 18:09:39.931 135-135/? E/CameraHAL﹕ Adapter state switch PREVIEW_ACTIVE Invalid Op! event = 0x5
07-06 18:09:40.173 135-135/? I/CameraClient﹕ Destroying camera 1

登录成功

07-06 18:13:21.868      135-502/? I/CameraClient﹕ Opening camera 1
07-06 18:13:21.868 135-502/? I/CameraHAL﹕ camera_device open
07-06 18:13:22.048 16009-19890/com...V/myTag﹕ Got front Camera
07-06 18:13:22.056 16009-19890/com...V/myTag﹕ rotation value = Automatic
07-06 18:13:22.063 16009-19890/com...V/myTag﹕ Set camera parameters
07-06 18:13:22.063 16009-19890/com...V/myTag﹕ Preview set
07-06 18:13:22.517 16009-19890/com...V/myTag﹕ Preview started
07-06 18:13:22.524 135-19893/? D/﹕ PPM: Standby to first shot: Sensor Change completed - :460.845 ms : 1404663202530 ms
07-06 18:13:23.517 16009-19890/com...V/myTag﹕ Thread slept for 1000ms
07-06 18:13:23.517 16009-19890/com...V/myTag﹕ Calling takePicture()
07-06 18:13:23.727 135-19893/? D/﹕ PPM: Shot to snapshot: :216.3 ms : 1404663203736 ms
07-06 18:13:23.774 135-19893/? D/﹕ PPM: Shot to Jpeg: :261.688 ms : 1404663203781 ms
07-06 18:13:23.837 16009-16009/com...D/myTag﹕ onPictureTaken() called // <-- This line is only diff from failure log
07-06 18:13:23.837 135-135/? E/CameraHAL﹕ Adapter state switch PREVIEW_ACTIVE Invalid Op! event = 0x5
07-06 18:13:24.001 135-135/? I/CameraClient﹕ Destroying camera 1

最佳答案

你用过表面支架吗?从 kitkat 开始,这是必要的,而不是一种选择。

也尝试将回调作为 Activity 的实例变量。

另请查看我遇到的类似问题,我可以使用我在此处提出的第一个解决方案来解决该问题。 Camera API not working on KITKAT

关于android - Camera onPictureTaken() 有时不被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24597931/

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