2012-04-24 33 views
0

我有一個Android相機應用程序的簡單拍攝照片類:如何從Android相機中的takePicture調用返回圖像數據字節數組?

public class SimplePicture implements Picturable, PictureCallback{ 

    Camera camera; 
    byte[] imgData; // image data in bytes 


    /** 
    *@param c, the camera instance that the Android phone is using. 
    */ 
    public SimplePicture(Camera c){ 
     this.camera = c; 

    } 

    public byte[] getPicture(int exposureCompensation) { 
     // TODO Auto-generated method stub 
     Parameters p = camera.getParameters(); 
     p.setExposureCompensation(exposureCompensation); 



     if(p.getMaxExposureCompensation() > 0){ // if exposure is supported 
      camera.takePicture(null, this, this); 
     } 


     return imgData; 


    } 

    public void onPictureTaken(byte[] data, Camera camera) { 
     // TODO Auto-generated method stub 
     imgData = data; 

    } 


} 

,你可能會看到我想有我Getpicture中()方法返回拍攝的圖像的字節數。由於回調函數是唯一讓我訪問imageData的函數,因此我知道當圖像數據在拍攝後準備就緒時,將調用回調函數。 onPictureTaken函數是否會同時運行到我的getPicture 函數中,以便在正確設置字節數組之前返回函數(return imgData)將會返回?或者執行是否等待onPictureTaken調用,然後返回?

如果是第二種情況,我想我的工作是正確的。如果是第一種情況,有人能帶領我走向正確的方向。有沒有更簡單的方法來做到這一點,還是我需要使用鎖定來確保我的函數按正確的順序執行?

謝謝

回答

1

沒有必要添加onPictureTaken之外的新方法()。 Captured中的圖像將從onPictureTaken()方法獲得byte [],這是您將獲得圖像的字節[]的地方。所以你可以將byte []轉換成Bitmap。您也可以使用下面的代碼片段獲取拍攝圖像的字節[]

private PictureCallback mPicture = new PictureCallback() { 

    @Override 
    public void onPictureTaken(final byte[] data, Camera camera) { 
     createBitmap(data); // Some stuffs to convert byte[] to Bitmap 
    } 
}; 
+0

對不起,如果我不清楚的問題。我正在實現一個具有getPicture方法的接口,它需要返回圖像數據的一個字節數組。原因是我也創建其他類(更復雜),將有getPicture,我會得到DataFlow作爲結果來實現一些圖像處理算法... – 2012-04-24 06:48:14

+0

@Saher創建你的接口內onPictureTaken()方法 – Venky 2012-04-24 06:50:05

+0

如何將那工作?我需要定義其他類,如HDR和Denoise,它們將遞歸調用getPicture方法,並實現相同的接口。我怎麼才能在裏面聲明接口? – 2012-04-25 01:58:56

相關問題