简体   繁体   English

从相机获取图像到ImageView Android

[英]Get Image from camera into ImageView Android

The following code works well to get the thumbnail of a picture taken from the camera. 以下代码适用于获取从相机拍摄的照片的缩略图。 I want to get the full image, not just the thumbnail. 我想获得完整的图像,而不仅仅是缩略图。 I have a Samsung Galaxy Nexus 我有三星Galaxy Nexus

private static final int CAMERA_PIC_REQUEST = 1111;

private ImageView mImage;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_camera);
        mImage = (ImageView) findViewById(R.id.imageView1);
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, CAMERA_PIC_REQUEST);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_PIC_REQUEST) {
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            mImage.setImageBitmap(thumbnail);
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
            try {
                file.createNewFile();
                FileOutputStream fo = new FileOutputStream(file);
                fo.write(bytes.toByteArray());
                fo.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

To get a full size picture you need to first save it to file and then extract it's bitmap from it, do this: 要获得完整大小的图片,您需要先将其保存到文件中,然后从中提取它的位图,执行以下操作:

 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
 File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
 startActivityForResult(intent, CAPTURE_NEW_PICTURE);

Then in onActivityResult : 然后在onActivityResult

 File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
 mImage = (ImageView) findViewById(R.id.imageView1);
 mImage.setImageBitmap(decodeSampledBitmapFromFile(file.getAbsolutePath(), 500, 250));

When decodeSampledBitmapFromFile : decodeSampledBitmapFromFile

    public static Bitmap decodeSampledBitmapFromFile(String path,
        int reqWidth, int reqHeight) { // BEST QUALITY MATCH

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        int inSampleSize = 1;

        if (height > reqHeight) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        }

        int expectedWidth = width / inSampleSize;

        if (expectedWidth > reqWidth) {
            //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }


    options.inSampleSize = inSampleSize;

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeFile(path, options);
  }

You can play with the numbers (500 and 250 in this case) to change the quality of the bitmap for the ImageView . 您可以使用数字(在本例中为500和250)来更改ImageView的位图质量。

暂无
暂无

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

相关问题 Android ImageView无法从图库或相机中获取图像 - Android ImageView can't get image from gallery or camera Android从相机在imageview中显示图像 - android display image in imageview from camera 从相机捕获的图像不在imageview android中显示 - Image captured from camera not displaying in imageview android 从相机获取图像或从图库上传并在 ImageView (Android Studio) 中显示后,保存图像为 SharedPreferences - Save image is SharedPreferences after Image is get from camera or upload from gallery and display at ImageView (Android Studio) 如何在图库中的imageView上设置图像以及在Android中由相机拍摄的图像? - How to set an image on imageView from the gallery and image taken by camera in Android? Android-如何放置从SD卡或相机胶卷加载图像的imageview? - Android - How to put an imageview that loads image from sdcard or camera roll? 从相机捕获图像在Android的ImageView中不显示? - Capture image from camera does not show in ImageView in Android? 如何在Dropbox的Camera Upload文件夹中将图像设置为Android中的ImageView? - How to Set image from Camera upload folder of dropbox to ImageView in android? 从图库或相机设置图像以适合Imageview android - set image from Gallery or Camera to fit to Imageview android Android - 从图库/相机获取图像时,Dialog不会刷新ImageView - Android - Dialog not refresing ImageView when getting image from gallery/ camera
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM