我有一个简单的 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 调用然后返回?
如果是第二种情况,我想我的工作是正确的。如果这是第一种情况,有人可以引导我走向正确的方向吗?有没有更简单的方法来执行此操作,或者我是否需要使用锁定来确保我的函数以正确的顺序执行?
谢谢
最佳答案
无需在 onPictureTaken() 之外添加新方法。在 Captured 中的图像之后,您将从 onPictureTaken() 方法获取 byte[],这是您获取图像的 byte[] 的地方。所以你可以将 byte[] 转换为 Bitmap。您也可以使用下面的代码 fragment 来获取捕获图像的 byte[]
private PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(final byte[] data, Camera camera) {
createBitmap(data); // Some stuffs to convert byte[] to Bitmap
}
};
关于java - 如何获取Android相机中takePicture调用返回的图像数据字节数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10292664/