【问题标题】:Android camera fails to take photo from background serviceAndroid相机无法从后台服务拍照
【发布时间】:2014-01-30 17:47:40
【问题描述】:

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

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 上的所有相机教程,但似乎没有任何效果。

【问题讨论】:

  • 1.服务不在后台线程中运行。要使用所需的后台服务,您必须扩展 IntentService。 2. 请向我们提供异常的堆栈跟踪。
  • 这是他可能得到的堆栈跟踪,因为我也有同样的问题
  • 你搞定了吗?
  • @dawidgdanski, 1. 我已经能够从主线程中的常规(不是IntentService)服务中拍照。 2. 他说没有抛出异常。
  • @Sam 也许这会有所帮助github.com/dawidgdanski/AndroidComponentsTests/tree/master/src/… 示例以 CaptureImage 开头...

标签: android service background camera photo


【解决方案1】:

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

更多信息请参见Taking picture from camera without preview

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多