获取捕获图像的位图?

时间:2014-09-08 09:15:02

标签: android bitmap android-file

我正在制作相机应用程序。我按照This教程捕获并保存设备中的图像。通过实现此功能,可以捕获图像并将其保存在设备的图库中。它将图像保存为文件。但我想得到捕获图像的位图。这是我的实现,

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera_layout);

    cameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
    filepath = Environment.getExternalStorageDirectory();

    if (checkCameraHardware(this)) {

        // Create an instance of Camera
        mCamera = getCameraInstance();

        try {
            // Get Camera Parameters
            Camera.Parameters params = mCamera.getParameters();

            // Set the Focus Mode
            params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
            mCamera.setParameters(params);

            mPreview = new CameraPreview(this, mCamera);

            FrameLayout preview = (FrameLayout) findViewById(R.id.cameraPreview);
            preview.addView(mPreview);
        } catch (Exception e) {
        }
    }

    Button captureButton = (Button) findViewById(R.id.button_capture);
    captureButton.setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // get an image from the camera
                mCamera.takePicture(null, null, mPicture);
            }
        }
    );
}

@Override
protected void onPause() {
    // TODO OnPause Method
    super.onPause();
    releaseCamera();
}

// TODO Detecting Camera Hardware
private boolean checkCameraHardware(Context context) {
    if (context.getPackageManager().hasSystemFeature(
            PackageManager.FEATURE_CAMERA)) {
        // This device has camera
        return true;
    } else {
        // No Camera on this Device
        return false;
    }
}

// TODO Accessing Camera
public static Camera getCameraInstance() {
    Camera c = null;

    try {
        c = Camera.open();
    } catch (Exception e) {
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}

private PictureCallback mPicture = new PictureCallback() {

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        // TODO Takes the picture and write to file
        File dir = new File(filepath.getAbsolutePath() + "/QR Codes/");
        dir.mkdirs();

        File pictureFile = getOutputMediaFile(FileColumns.MEDIA_TYPE_IMAGE);

        if (pictureFile == null) {
            Log.d("PICFILE",
                    "Error creating media file, check storage permissions");
            return;
        }

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
            updateGallery();
        } catch (Exception e) {
        }
    }
};

/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "MyCameraApp");

    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");
    } else {
        return null;
    }
    return mediaFile;
}

2 个答案:

答案 0 :(得分:1)

将图片从文件转换为Bitmap。像,

Bitmap bmp = BitmapFactory.decodeFile(urImageFilePath);

答案 1 :(得分:0)

尝试:

Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data.length);