简体   繁体   English

如何存储使用camera.takePicture(null,null,mPicture)捕获的图像,以便可以在下一个活动中显示它?

[英]How to store image captured using camera.takePicture(null,null,mPicture) so that it can be displayed it in next activity?

I am making a custom camera application in android. 我在android中制作自定义camera应用程序。 I capture the image using 我使用捕获image

camera.takePicture(null,null,mPicture)" and "Camera.PictureCallBack()

I want to store the image taken from the camera so that I can display it in the next activity . 我想存储从camera拍摄的image ,以便我可以在下一个activity显示它。 How can it be done? 如何做呢?

    Camera.PictureCallback mPicture = new Camera.PictureCallback() {

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        File pictureFile = getOutputMediaFile();
        if (pictureFile == null) {
            return;
        }
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);

            fos.close();
        } catch (FileNotFoundException e) {

        } catch (IOException e) {
        }
    }
};



private static File getOutputMediaFile() {
    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;
        }
    }
    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
            .format(new Date());
    File mediaFile;
    mediaFile = new File(mediaStorageDir.getPath() + File.separator
            + "IMG_" + timeStamp + ".jpg");

    return mediaFile;
}

Pass uri in extras of your next activity intent 将uri传递给您下一个活动意图的额外内容

public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_PICTURE_USING_CAMERA) {
            try {
               Intent i = new Intent(FirstActivity.this, SecondActivity.class);
               i.putExtra("uri",outputFileUri);
               startActivity(i);
            } catch (Exception ex) {
                Log.e("Exception", ex.toString());
            }
        }
    }
}

then retrieve image in your second activity 然后在第二个活动中检索图像

Uri = getIntent().getExtras("uri");

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;

final Bitmap capturedimage = BitmapFactory.decodeFile(uri, options);
imgPreview.setImageBitmap(capturedimage );

TO open Camera 打开相机

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, SELECT_PICTURE_USING_CAMERA);

Then need to implement onActivityResult to get the data. 然后需要实现onActivityResult来获取数据。

public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_PICTURE_USING_CAMERA) {
            try {
               final File file = new File(outputFileUri.getPath());
            } catch (Exception ex) {
                Log.e("Exception", ex.toString());
            }
        }
    }
}

Then you can take the image form the file path to display in the next activity. 然后,您可以从file路径中获取图像以在下一个活动中显示。

You can use CameraView to implement the custom camera in very simple way which gives you the functionalities to capture image and video. 您可以使用CameraView以非常简单的方式实现自定义相机,从而为您提供捕获图像和视频的功能。 You can see the DEMO where it the CameraActivity tooks the picture and PicturePreviewActivity and VideoPreviewActivity are use to preview the picture and video taken in CameraActivity. 您可以看到CameraActivity拍摄照片的DEMOPicturePreviewActivityVideoPreviewActivity用于预览CameraActivity拍摄的照片和视频。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM