我遇到了问题,我希望捕获预览并将其作为Callable的结果返回...
package ro.ubbcluj.digitrecognitionapp;
import java.io.ByteArrayOutputStream;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.util.Log;
public class CameraCapturingTask implements Callable<Bitmap> {
private Camera camera;
private Bitmap imageBitmap;
private CountDownLatch callBackFinished = new CountDownLatch(1);
CameraCapturingTask(Camera camera) {
this.camera = camera;
}
@Override
public Bitmap call() throws Exception {
Log.d("CapturePreviewTask", "Async Task started");
try {
camera.setOneShotPreviewCallback(pictureCallback);
callBackFinished.await(); // it blocks here!!!
} catch (Exception e) {
if (e instanceof InterruptedException) {
Log.d("PreviewCapturingTask",
"Task caught InterruptedException ,ok");
}
}
return imageBitmap;
}
private PreviewCallback pictureCallback = new PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
Log.d("CameraPreview", "picture taken");
getBitmap(data, camera);
callBackFinished.countDown();
}
};
private void getBitmap(byte[] data, Camera camera) {
Log.d("Async Task", "Processing Image ");
Camera.Parameters parameters = camera.getParameters();
int width = parameters.getPreviewSize().width;
int height = parameters.getPreviewSize().height;
YuvImage yuv = new YuvImage(data, parameters.getPreviewFormat(), width,
height, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
yuv.compressToJpeg(new Rect(0, 0, width, height), 50, out);
byte[] bytes = out.toByteArray();
imageBitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
}
阻止我等待CountDownLatch ...如果我评论callBackFinished.await();
,则在Thread返回后调用回调。
如何解决此问题并返回捕获预览的位图?