拍摄一张照片然后将其显示到ImageView

时间:2015-09-12 12:40:23

标签: android camera

我想拍一张照片并把它放在一个imageView中。我已经设法打开相机,每次拍照时都可以拍下另一张照片。 这是我的onCreate方法:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);
    File photo = dispatchTakePictureIntent();
    ImageView imgView = (ImageView) findViewById(R.id.DisplayImageView);
    if(photo.exists()){
        Bitmap myBitmap = BitmapFactory.decodeFile(photo.getAbsolutePath());
        imgView.setImageBitmap(myBitmap);
    }
}

这是我的拍照方式:

    private File dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File photoFile = null;
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            Toast.makeText(this, "Failed to create the image file!", Toast.LENGTH_SHORT).show();
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);

        }
    }
    return photoFile;
}

我需要在一张照片后从相机返回活动并查看它。我怎么能这样做?

3 个答案:

答案 0 :(得分:1)

您需要覆盖Activity的onActivityResult()以获取图像并在ImageView中显示它。当您从Camera获取图像时,您应该根据需要对图像进行采样,并且为了防止图像在ImageView中显示时旋转,您需要查找Exif参数。

这是onActivityResult()代码。

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        if (resultCode == RESULT_OK && requestCode == Constants.REQUEST_TAKE_PHOTO) {

// in case of taking pic from camera, you have to define filepath already  and image get saved in that filepath you specified at the time when you started camera intent.So in your case it will be following

           String filePath=photoFile.getAbsolutePath(); //photofile is the same file you passed while starting camera intent.
            if (filePath != null) {
                int orientation = 0;
                private Bitmap imageBitmap;
                try {
                    ExifInterface exif = new ExifInterface(filePath);
                    orientation = exif.getAttributeInt(
                            ExifInterface.TAG_ORIENTATION, 1);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    final BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(filePath, options);
                    options.inSampleSize = calculateInSampleSize(
                            options, reqWidth, rewHeight);
                    options.inJustDecodeBounds = false;
                    imageBitmap = BitmapFactory.decodeFile(filePath,
                            options);
                    if (orientation == 6) {
                        Matrix matrix = new Matrix();
                        matrix.postRotate(90);
                        imageBitmap = Bitmap.createBitmap(imageBitmap,
                                0, 0, imageBitmap.getWidth(),
                                imageBitmap.getHeight(), matrix, true);
                    } else if (orientation == 8) {
                        Matrix matrix = new Matrix();
                        matrix.postRotate(270);
                        imageBitmap = Bitmap.createBitmap(imageBitmap,
                                0, 0, imageBitmap.getWidth(),
                                imageBitmap.getHeight(), matrix, true);
                    } else if (orientation == 3) {
                        Matrix matrix = new Matrix();
                        matrix.postRotate(180);
                        imageBitmap = Bitmap.createBitmap(imageBitmap,
                                0, 0, imageBitmap.getWidth(),
                                imageBitmap.getHeight(), matrix, true);
                    }
                } catch (OutOfMemoryError e) {
                    imageBitmap = null;
                    e.printStackTrace();
                } catch (Exception e) {
                    imageBitmap = null;
                    e.printStackTrace();
                }
            }
            if (imageBitmap != null) {
                // set this imageBitmap to your ImageView
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}

这是采样函数

public static int calculateInSampleSize(BitmapFactory.Options options,
                                        int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;
    if (height > reqHeight || width > reqWidth) {
        final int halfHeight = height / 2;
        final int halfWidth = width / 2;
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }
    return inSampleSize;
}

答案 1 :(得分:0)

在您的Activity中,startActivityForResult会覆盖以下方法

create

将mImageView替换为要显示图像的ImageView

答案 2 :(得分:0)

相机拍摄的图像可能太大,无法直接在ImageView中显示。

在将图像显示在ImageView中之前,请检查此链接以有效地缩放图像。

此代码与您希望在应用程序中实现的代码完全相同:

http://android-er.blogspot.co.at/2012/07/scale-bitmap-efficiently.html?m=1