2012-02-02 103 views
11

一旦調用Camera.takePicture(),我的預覽將停止更新,如文檔中所述。檢測圖像捕獲過程完成並調用startPreview()使其再次開始更新的最佳方法是什麼?捕獲圖像後調用StartPreview()的最佳方式是什麼?

呼叫不能被放置在任何傳遞給takePicture的回調,根據文檔,因爲他們應該在我引用這一切都回來了。

我目前最好的猜測是建立一個處理程序和發佈延遲的Runnable,將其從JPEG回調(或取其最最後定義的回調返回)。

回答

6

你應該從的AsyncTask(或線程),然而AsyncTaks的是更容易的選擇中啓動mCamera.takePicture。

一個非常簡單的實現(這當然可以被修改)是:

稱爲拍攝照片的方法

/** 
* 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()); 
     } 
    } 
}; 
+0

那麼,是他們在做什麼在本教程中只錯了嗎? http://developer.android.com/training/camera/cameradirect.html我真的不明白他們怎麼重啓預覽存在,但它看上去一點也不像的AsyncTask或任何... – Headcrab 2012-06-27 08:19:21

12

由於PictureCallback在一個單獨的線程啓動反正(它不會鎖定用戶界面),你不需要使用的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(){ 
    ... 
}); 

兩個有他們的用途,取決於您的需求。

+0

對於'PictureCallback'上下工夫一個後臺線程,攝像頭應該在單獨的事件線程中打開,參見[代碼示例](http://stackoverflow.com/a/19154438/192373)。 – 2014-02-12 13:05:52

+0

@Alex我的理解是PictureCallback()和PreviewCallback()(你在文章中引用的)操作方式不同。如果您想對PictureCallback()生成的圖像進行任何處理,請使用該線程。 如果我對我的理解不正確,請讓我知道。 – wblaschko 2014-02-20 19:00:57

+0

這很容易檢查,但我不知道答案了手 – 2014-02-20 19:30:36

相關問題