0

我有一个用于 android 相机应用程序的简单拍照类:

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 函数同时运行,以便在正确设置字节数组之前返回函数(返回 imgData)?还是执行等待 onPictureTaken 调用然后返回?

如果是第二种情况,我想我的工作是正确的。如果是第一种情况,有人可以引导我走向正确的方向。有没有更简单的方法可以做到这一点,还是我需要使用锁定来确保我的函数以正确的顺序执行?

谢谢你

4

1 回答 1

1

无需在 onPictureTaken() 之外添加新方法。在 Captured 中的图像之后,您将从 onPictureTaken() 方法中获得 byte[],这是您将获得 Image 的 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
    }
};
于 2012-04-24T06:44:56.863 回答