将图像转换为位图并减小其大小

时间:2013-07-18 11:56:23

标签: android bitmap scaling image-resizing bitmapimage

我已经看过很多不同的方法来创建一个缩小尺寸的图像位图,但是它们都没有正常工作/我需要不同的东西。

这有点难以解释: - )

我需要的是一个位图,它保持图片的比例,但小于一定的大小 - 例如1mb或像素尺寸的等价物(因为这个位图需要添加为意图的putExtra())。

到目前为止我遇到的问题:

我看过的大多数方法都会创建位图的缩放版本。所以:图像 - > Bitmap1(未缩放) - > Bitmap2(缩放)。但是如果图像的分辨率非常高,则不能缩小尺寸。我认为解决方案是创建一个精确大小的位图,以便可以减少任何分辨率。

然而,这种方法的副作用是,已经小于所需大小的图像将被调整大小(或者调整大小不起作用?)。所以需要一个“if”来检查图像是否可以转换为位图而不需要调整大小。

我不知道如何去做这个,所以非常感谢任何帮助! : - )

这就是我现在正在使用的东西(我不希望它这样做):

// This is called when an image is picked from the gallery
@Override
    public void onActivityResult(int requestCode, int resultCode,
            Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

        switch (requestCode) { 
        case 0:
            if (resultCode == Activity.RESULT_OK) {
                selectedImage = imageReturnedIntent.getData();
                viewImage = imageReturnedIntent.getData();

                try {
                    decodeUri(selectedImage);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                iv_preview.setImageBitmap(mImageBitmap);
            }
            break; // The rest is unnecessary

这是当前缩放尺寸的部分:

private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException { 


        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true; //
        BitmapFactory.decodeStream(getActivity().getContentResolver()
                .openInputStream(selectedImage), null, o);

        // The new size we want to scale to
        final int REQUIRED_SIZE = 260; // Is this kilobites? 306

        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
                break;
            }
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }


        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        o2.inScaled = false; // Better quality?
        mImageBitmap = BitmapFactory.decodeStream(getActivity()
                .getContentResolver().openInputStream(selectedImage), null, o2);
        return BitmapFactory.decodeStream(getActivity().getContentResolver()
                .openInputStream(selectedImage), null, o2);

    }

如果需要更多解释,请说。

谢谢

1 个答案:

答案 0 :(得分:3)

如何致电:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        pho1.setImageBitmap(decodeSampledBitmapFromResource(picturePath,
                80, 60));

方法:

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) {

            // Calculate ratios of height and width to requested height and
            // width
            final int heightRatio = Math.round((float) height
                    / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);

            // Choose the smallest ratio as inSampleSize value, this will
            // guarantee
            // a final image with both dimensions larger than or equal to the
            // requested height and width.
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }

        return inSampleSize;
    }

    public static Bitmap decodeSampledBitmapFromResource(String path,
            int reqWidth, int reqHeight) {
        Log.d("path", path);
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

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

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(path, options);
    }