缩小照片采取照相机和获取图片路径 [英] Scaling down photo taken by camera and getting the picture Path

查看:135
本文介绍了缩小照片采取照相机和获取图片路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图扩展用相机拍摄的照片,但我不知道在哪里或如何做到这一点。眼下code正在访问的摄像头,拍照和列表视图显示出来,我想也得到的图片路径,但我不确定如何做到这一点以及。任何帮助将是非常美联社preciated。

I am trying to scale the photos taken with a camera, but i am not sure where or how to do this. Right now the code is accessing the camera, taking a picture and displaying it in a listview, i would like to also get the picture path but am unsure how to do this aswell. Any help would be highly appreciated.

/**
     * This function is called when the add player picture button is clicked.
     * It accesses the devices gallery and the user can choose a picture
     * from the gallery.
     * Or if the user chooses to take a picture with the camera, it handles that
     */

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case TAKE_PICTURE:
            if (resultCode == Activity.RESULT_OK) {
                Uri selectedImage = imageUri;
                getContentResolver().notifyChange(selectedImage, null);
                ContentResolver cr = getContentResolver();
                this.picPath = selectedImage.getPath();
                Bitmap bitmap;
                try {
                     bitmap = android.provider.MediaStore.Images.Media
                     .getBitmap(cr, selectedImage);
                    imageView = (ImageView) findViewById(R.id.imagePlayer); 
                    imageView.setImageBitmap(bitmap);
                    Toast.makeText(this, selectedImage.toString(),
                            Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                            .show();
                    Log.e("Camera", e.toString());
                }
            }

感谢

推荐答案

得到位图图像后,您可以使用来自Bitmap类createScaledBitmap静态方法

After getting bitmap image you can use createScaledBitmap static method from Bitmap class

   Bitmap.createScaledBitmap(yourBitmap, 50, 50, true); // Width and Height in pixel e.g. 50

但在未来的极端内存不足情况...
 如果你不小心,位图可以快速消耗可用内存的预算,导致应用程序崩溃是由于可怕的例外:
java.lang.OutofMemoryError:位图的大小超过VM预算

所以要避免 java.lang.OutOfMemory例外,解码之前,检查一个位图的尺寸,除非你绝对信任其来源为您提供了舒适适合predictably大小的图像数据内的可用存储器

so To avoid java.lang.OutOfMemory exceptions, check the dimensions of a bitmap before decoding it, unless you absolutely trust the source to provide you with predictably sized image data that comfortably fits within the available memory.

  // below 3 line of code will come instead of 
//imageView.setImageBitmap(bitmap);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();         
    photo.compress(Bitmap.CompressFormat.JPEG,100,stream);
    imageView.setImageBitmap(decodeSampledBitmapFromByte(stream.toByteArray(),50,50));  

的BitmapFactory类提供了多种解码方法(德codeByteArray的(),德codeFILE(),德codeResource(),等等),用于创建来自各种源位图。您可根据您的影像数据源的最合适的德code方法。这些方法尝试为构造位图分配内存,因此可以很容易地导致内存不足异常。每种类型的德code的方法有让您指定通过BitmapFactory.Options类解码选项的附加签名。在inJustDe codeBounds属性设置为true,而解码避免了内存分配,返回null位图对象,但设置outWidth,outHeight和outMimeType。这种技术允许你之前的位图的构造(以及存储器分配)读出的图像数据的尺寸和类型。

The BitmapFactory class provides several decoding methods (decodeByteArray(), decodeFile(), decodeResource(), etc.) for creating a Bitmap from various sources. Choose the most appropriate decode method based on your image data source. These methods attempt to allocate memory for the constructed bitmap and therefore can easily result in an OutOfMemory exception. Each type of decode method has additional signatures that let you specify decoding options via the BitmapFactory.Options class. Setting the inJustDecodeBounds property to true while decoding avoids memory allocation, returning null for the bitmap object but setting outWidth, outHeight and outMimeType. This technique allows you to read the dimensions and type of the image data prior to construction (and memory allocation) of the bitmap.

 // please define following two methods in your activity
    public Bitmap decodeSampledBitmapFromByte(byte[] res,
                int reqWidth, int reqHeight) {

            // First decode with inJustDecodeBounds=true to check dimensions
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeByteArray(res, 0, res.length,options);

            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeByteArray(res, 0, res.length,options);
        }
         public 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) {
                if (width > height) {
                    inSampleSize = Math.round((float)height / (float)reqHeight);
                } else {
                    inSampleSize = Math.round((float)width / (float)reqWidth);
                }
            }
            return inSampleSize;
        }

和请参见于Android培训任何相关的位图下面的链接
java.lang.OutofMemoryError:位图的大小超过VM预算
http://developer.android.com/training/displaying-bitmaps/load -bitmap.html

And please refer following link from Android Training any bitmap related java.lang.OutofMemoryError: bitmap size exceeds VM budget http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

这篇关于缩小照片采取照相机和获取图片路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆