gpt4 book ai didi

Android 相机无法从后台服务拍照

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:15:38 33 4
gpt4 key购买 nike

我已经实现了一项从后台线程拍摄照片的服务,但照片从未在我的任何设备上拍摄过......这是代码(下面的日志输出):

public class PhotoCaptureService extends Service {
private static final String TAG = "PhotoCaptureService";

@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.d(TAG, "Starting the PhotoCaptureService");
takePhoto();
}

private void takePhoto() {

Log.d(TAG, "Preparing to take photo");
Camera camera = null;

try {

camera = Camera.open();

} catch (RuntimeException e) {

Log.e(TAG, "Camera not available", e);
return;
}

if (null == camera) {

Log.e(TAG, "Could not get camera instance");
return;
}

Log.d(TAG, "Got the camera, creating the dummy surface texture");
SurfaceTexture dummySurfaceTexture = new SurfaceTexture(0);

try {

camera.setPreviewTexture(dummySurfaceTexture);

} catch (Exception e) {

Log.e(TAG, "Could not set the surface preview texture", e);
}

Log.d(TAG, "Preview texture set, starting preview");

camera.startPreview();

Log.d(TAG, "Preview started");

camera.takePicture(null, null, new Camera.PictureCallback() {

@Override
public void onPictureTaken(byte[] data, Camera camera) {

Log.d(TAG, "Photo taken, stopping preview");

camera.stopPreview();

Log.d(TAG, "Preview stopped, releasing camera");

camera.release();

Log.d(TAG, "Camera released");
}
});
}

记录输出:

D/PhotoCaptureService﹕ Starting the PhotoCaptureService
D/PhotoCaptureService﹕ Preparing to take photo
D/PhotoCaptureService﹕ Got the camera, creating the dummy surface texture
D/PhotoCaptureService﹕ Preview texture set, starting preview
D/PhotoCaptureService﹕ Preview started

此时没有其他事情发生,永远不会调用 onPictureTaken 方法,也不会抛出任何错误或异常。有谁知道为什么会这样?我查看了 StackOverflow 上的所有相机教程,但似乎没有任何效果。

最佳答案

根据我的经验和阅读的内容,虚拟 SurfaceTexture 策略并不适用于所有手机。尝试添加一个 1x1 像素的 SurfaceView 并在 SurfaceView.getHolder()onSurfaceCreated 回调中开始预览(通过 addCallback 添加)。

参见 Taking picture from camera without preview获取更多信息。

关于Android 相机无法从后台服务拍照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21463796/

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