Cwac Camera添加缩略图

时间:2014-03-05 09:37:09

标签: android camera commonsware-cwac

我想用cwac库在相机视图上缩略图。

cameraFragment.takePicture();

            Bitmap bitmap = Utility.decodeSampledBitmapFromPath(
                    cameraFragment.cameraHost.getPhotoPath()
                            .getAbsolutePath(), 120, 120);
            image.setImageBitmap(bitmap);

2 个答案:

答案 0 :(得分:1)

我和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

答案 1 :(得分:0)

找到解决方案: 我不使用cwac相机库。使用自定义相机布局。 Kut Camera