2012-04-24 37 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

相关问题