【问题标题】:What's the best way to call StartPreview() after an image is captured?捕获图像后调用 StartPreview() 的最佳方法是什么?
【发布时间】:2012-02-25 08:41:42
【问题描述】:

一旦调用 Camera.takePicture(),我的预览将停止更新,如文档中所述。检测图像捕获过程已完成并调用 startPreview() 使其再次开始更新的最佳方法是什么?

根据文档,不能将调用放在传递给 takePicture 的任何回调中,因为它们应该在我调用它之前都已返回。

我目前最好的猜测是创建一个处理程序并从 JPEG 回调(或最后定义的要返回的回调中的任何一个)向其发布延迟的 Runnable。

【问题讨论】:

    标签: android camera


    【解决方案1】:

    由于 PictureCallback 无论如何都是在单独的线程中启动的(它不会锁定 UI),因此您不需要使用 AsyncTask 来调用捕获。

    有两种方法可以做你想做的事,最简单的方法如下:

    mCamera.startPreview(); //preview has to be started before you can take a picture
    mCamera.takePicture(null, null, mPictureCallback); //take a picture
    
    private PictureCallback mPictureCallback = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            camera.startPreview(); //once your camera has successfully finished
                                   //its capture it's safe to restart the preview
            ... //anything else you want to do to process that image
        }
    }
    

    第二种是使用匿名函数,例如:

    mCamera.takePicture(null, null, new PictureCallback(){
        ...
    });
    

    两者都有其用途,具体取决于您的需求。

    【讨论】:

    • 要让PictureCallback 在后台线程上工作,相机应该在单独的事件线程中打开,参见code example
    • @Alex 我的理解是 PictureCallback() 和 PreviewCallback() (您在帖子中引用)的操作方式不同。如果您想对 PictureCallback() 生成的图像做任何工作,请使用线程。如果我的理解有误,请告诉我。
    • 这很容易检查,但我不知道答案
    • @AlexCohn 感谢您对此的反馈,它帮助我从我的应用中挤出更多性能。
    【解决方案2】:

    您应该从 AsyncTask(或线程)中启动 mCamera.takePicture,但是 AsyncTaks 是更简单的选择。

    一个非常简单的实现(当然可以修改)是:

    拍照调用的方法

    /**
     * Execute the AsyncTask that will handle the preview of the captured photo.
     */
    public void takePicture() {
        TakePictureTask takePictureTask = new TakePictureTask();
        takePictureTask.execute();
    }
    

    AsyncTask 子类

    /**
     * A pretty basic example of an AsyncTask that takes the photo and
     * then sleeps for a defined period of time before finishing. Upon
     * finishing, it will restart the preview - Camera.startPreview().
     */
    
    private class TakePictureTask extends AsyncTask<Void, Void, Void> {
    
        @Override
        protected void onPostExecute(Void result) {
            // This returns the preview back to the live camera feed
            mCamera.startPreview();
        }
    
        @Override
        protected Void doInBackground(Void... params) {
            mCamera.takePicture(null, null, mPictureCallback);
    
            // Sleep for however long, you could store this in a variable and
            // have it updated by a menu item which the user selects.
            try {
                Thread.sleep(3000); // 3 second preview
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            return null;
        }
    
    }
    

    PictureCallback 字段

    private PictureCallback mPictureCallback = new PictureCallback() {
    
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            File file = null;
    
            // Check whether the media is mounted with read/write permission.
            if (Environment.MEDIA_MOUNTED.equals(
                    Environment.getExternalStorageState())) {
                file = getOutputMediaFile();
            }
    
            if (file == null) {
                Log.d(TAG, "Error creating media file, check storage persmissions!");
                return;
            }
    
            try {
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                fileOutputStream.write(data);
                fileOutputStream.close();
            } catch (FileNotFoundException e) {
                Log.d(TAG, "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d(TAG, "Error accessing file: " + e.getMessage());
            }
        }
    };
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多