Android的摄像头未采取从后台服务照片 [英] Android camera fails to take photo from background service

查看:409
本文介绍了Android的摄像头未采取从后台服务照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个服务才能从后台线程的图片,但照片永远不会采取任何我的设备......这里是code(下日志输出):

I've implemented a service to take a picture from a background thread, but the photo never gets taken on any of my devices... here is the code (logging output below):

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方法不会被调用,也没有抛出错误或异常。有谁知道为什么发生这种情况?我看着在计算器上每个摄像头的教程和似乎没有任何工作。

At this point nothing else happens, the onPictureTaken method never gets called and there is no error or exception thrown. Does anyone know why this is happening? I've looked at every camera tutorial on StackOverflow and nothing seems to work.

推荐答案

从我的经验和我读过,虚拟表面纹理策略并不所有的工作对手机。尝试,而不是添加1x1像素 SurfaceView 并开始在 SurfaceView.getHolder()的<在preVIEW code> onSurfaceCreated 回调(通过添加的addCallback )。

From my experience and what I've read, the dummy SurfaceTexture strategy doesn't work on all phones. Try instead adding a 1x1 pixel SurfaceView and starting the preview in the SurfaceView.getHolder()'s onSurfaceCreated callback (added via addCallback).

请参阅从相机拍照,而不preVIEW 了解详情。

这篇关于Android的摄像头未采取从后台服务照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆