Android的摄像头未采取从后台服务照片摄像头、后台、照片、Android

由网友(一声姐妹一生姐妹)分享简介:我已经实现了一个服务才能从后台线程的图片,但照片永远不会采取任何我的设备......这里是code(下日志输出):I've implemented a service to take a picture from a background thread, but the photo never gets taken...

我已经实现了一个服务才能从后台线程的图片,但照片永远不会采取任何我的设备......这里是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()的 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 了解详情。

阅读全文

相关推荐

最新文章