我编写了一个程序以完成相机叠加,我发现当我在onPictureTaken()中设置变量“image_data”时,它不会立即更改值,它会在我再次调用onPictureTaken时更改值 我无法解决它,有人可以帮助我吗?
private PictureCallback jpegCallback = new PictureCallback()
{public void setImageData(byte[] b) {
image_data = b;
}
@Override
public void onPictureTaken(byte[] data, Camera camera)
{
if (mCamera != null){
mCamera.startPreview();}
FileOutputStream outStream = null;
try {
// Write to SD Card
outStream = new FileOutputStream(String.format("/sdcard/%d.jpg",
System.currentTimeMillis()) );
outStream.write(data);
outStream.close();
Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
Log.d(TAG, String.valueOf(data));
image_data = data;
setImageData(data); //neither direct assign nor setImageData are invalid
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Log.d(TAG, "onPictureTaken - jpeg");
}
};
CameraTestActivity代码
mCamera.takePicture();
Toast toast = Toast.makeText(CameraTestActivity.this,
"PhotoTaken"+mCamera.getImageData(), Toast.LENGTH_LONG);
toast.show();
并且第一张拍摄的照片是“PhotoTaken null” 次要输出是“PhotoTaken B @ c4f05e2”
以下是logcat数据
12-03 22:19:20.416 21589-21589/com.example.acer.camerapreview
D/CameraTest.VersionedCamera: onPictureTaken - wrote bytes: 1644369
12-03 22:19:20.416 21589-21589/com.example.acer.camerapreview
D/CameraTest.VersionedCamera: [B@c4f05e2
12-03 22:19:29.631 21589-21589/com.example.acer.camerapreview
D/CameraTest.VersionedCamera: onPictureTaken - wrote bytes: 1622238
12-03 22:19:29.631 21589-21589/com.example.acer.camerapreview
D/CameraTest.VersionedCamera: [B@adb9e2e
答案 0 :(得分:2)
mCamera.takePicture()
需要传递PictureCallback
,那么您在哪里传递PictureCallback
(在您的代码中,我看到您的callback
是jpegCallback
)?
Toast.makeText(CameraTestActivity.this, "PhotoTaken" + mCamera.getImageData(), Toast.LENGTH_LONG).show()
它应该在PictureCallback
中正确写入,而不是在它下面。
例如:
mCamera.takePicture(null, null, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
// your code in here is correct ✔︎
}
});
但是
mCamera.takePicture(null, null, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
}
});
// your code in here is wrong ✕