我想使用 cwac 库在相机 View 中缩略图。
cameraFragment.takePicture();
Bitmap bitmap = Utility.decodeSampledBitmapFromPath(
cameraFragment.cameraHost.getPhotoPath()
.getAbsolutePath(), 120, 120);
image.setImageBitmap(bitmap);
最佳答案
我在 Cwac 上遇到了同样的问题,我的工作太先进了,无法切换到另一个库,所以我的解决方案就在这里。
修改您的 CameraFragment.onCreate 方法以将您的 DemoCameraHost 设置为默认主机:
public class CameraFragment extends com.commonsware.cwac.camera.CameraFragment {
private DemoCameraHost mDemoCamHost;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
setHasOptionsMenu(true);
mDemoCamHost = new DemoCameraHost(getActivity());
this.setHost(mDemoCamHost);
}
在 DemoCameraHost 方法中,useSingleShotMode 必须返回 false
。
/**
* Method indicates if after taking picture bitmap is frozen or next frame shown.
*
* @return Enable showing preview, must be false for this project.
*/
@Override
public boolean useSingleShotMode() {
return false;
}
现在您必须覆盖 DemoCameraHost 中的 saveImage 方法,您在其中接收编码为字节数组的图像,只需将其解码回来:
@Override
public void saveImage(PictureTransaction xact, byte[] image) {
Log.i(TAG, "saveImage");
Bitmap bm = BitmapFactory.decodeByteArray(image, 0, image.length);
//drawOnTop.setBitmap(bm); // this is my internal class that operates on the bitmap
if (bm == null)
Log.e(TAG, "bitmap is null");
else
Log.e(TAG, "bitmap size: " + bm.getWidth() + ":" + bm.getHeight());
// dont'save image on SD, prevents delay and freezing screen
//super.saveImage(xact, image);
}
现在,当您从 CameraFragment 调用 takePicture()
时,您将获得:
04-06 11:47:12.576 3038-3038/net.agilob.ssocv I/CamFrg﹕ useSingleShotMode
04-06 11:47:12.596 3038-3130/net.agilob.ssocv I/CamFrg﹕ saveImage
04-06 11:47:12.676 3038-3130/net.agilob.ssocv E/CamFrg﹕ bitmap size: 1280:960
关于android - Cwac 相机添加缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22193793/