Caa*_*los 5 android android-camera
我正在为Android构建一个API,开发人员可以使用我的类来拍照.但是,如何使用onPictureTaken返回一个Bitmap.看看我的代码,请:
这是mainActivity上的一个按钮:
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// onPictureTaken does not return nothing
// so, how can I get a bitmap from takePicture?
Bitmap mBitmap = CameraUtils.takePicture();
}
});
Run Code Online (Sandbox Code Playgroud)
这是我的类CameraUtils:
public class CameraUtils implements Camera.PictureCallback {
private Camera mCamera = null;
private SurfaceTexture surfaceTexture = new SurfaceTexture(0);
public Bitmap takePicture() {
try {
mCamera = Camera.open(1);
Camera.Parameters params = mCamera.getParameters();
List<Camera.Size> sizes = params.getSupportedPictureSizes();
params.setPictureFormat(ImageFormat.JPEG);
params.setPictureSize(sizes.get(0).width, sizes.get(0).height);
mCamera.setParameters(params);
mCamera.setPreviewTexture(surfaceTexture);
mCamera.startPreview();
Log.i("MyCamera", "before takePicture");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mCamera.takePicture(null, null, CameraUtils.this);
}
}, 1000);
//how to return a bitmap from here??
//how to wait for onPictureTaken??
return ?????
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap mBitmap = BitmapFactory
.decodeByteArray(data, 0, data.length);
//
// how to return something here??
//
//
// What logic I have to do?
//
//
//
// return mBitmap;
//
}
}
Run Code Online (Sandbox Code Playgroud)
我需要做什么才能使用我的类'方法获取一个Bitmap?
谢谢.
| 归档时间: |
|
| 查看次数: |
1691 次 |
| 最近记录: |