图像调整大小问题

时间:2012-06-19 08:58:11

标签: android bitmap

我正在根据this链接调整图片大小。
图像尺寸为3264x2448,计算后,sampleize现在为24,但图像旋转90度(左)。
当宽度大于高度时发生。我被困了。无法解决它。

I am using accroding to http://developer.android.com/training/displaying-bitmaps/load-bitmap.html


public static String getCompressedImagePath(String orgImagePath,
        String storeImagePath) {
    if (orgImagePath == null) {
        return null;
    }
    Bitmap bitmap = decodeSampledBitmapFromResource(orgImagePath, 100, 100);
    String absolutePath = "";
    FileOutputStream fos = null;
    try {

        fos = new FileOutputStream(storeImagePath);
        bitmap.compress(getCompressionFormatType(orgImagePath),
                IMAGE_COMPRESS_FACTOR, fos);
        fos.flush();
        absolutePath = storeImagePath;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fos != null) {
                fos.close();
                fos = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return absolutePath;
}

public static Bitmap decodeSampledBitmapFromResource(String orgImagePath,
        int reqWidth, int reqHeight) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(orgImagePath, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(orgImagePath, options);
}

public static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    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;
}

并且已从链接复制decodeSampledBitmapFromResource方法并将压缩图像设置为intoimageview。

1 个答案:

答案 0 :(得分:1)

我用过你的代码&用于我的示例应用程序&它工作正常。所以你运行这个应用程序&找出你还缺少什么

ACTIVITY-ImageScaleTestActivity


    package com.myTutororial;

    import java.io.File;

    import com.myTutororial.utils.ImageUtils;

    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;

    public class ImageScaleTestActivity extends Activity {
        final String SRC_IMAGE = "/sdcard/Gallary/2.png";
        final String CONVERTED_IMAGE = "/sdcard/Gallary/Converted/1.png";
        boolean isConvertedImage;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            Button button = (Button) findViewById(R.id.button1);
            button.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    isConvertedImage= !isConvertedImage;
                    if(isConvertedImage){
                        setImage(CONVERTED_IMAGE);
                    }else{
                        setImage(SRC_IMAGE);
                    }
                }
            });
            setImage(SRC_IMAGE);
            ImageUtils.getCompressedImagePath(SRC_IMAGE, CONVERTED_IMAGE);
        }

        public void setImage(String imagePath) {
            File imgFile = new File(imagePath);
            if (imgFile.exists()) {

                Bitmap myBitmap = BitmapFactory.decodeFile(imgFile
                        .getAbsolutePath());

                ImageView myImage = (ImageView) findViewById(R.id.imageView1);
                myImage.setImageBitmap(myBitmap);

            }

        }
    }

XML-main.xml中


    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:src="@drawable/ic_launcher" android:layout_weight="1"/>

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show converted Image" />

    </LinearLayout>

IMAGEUTILS


    package com.myTutororial.utils;

    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;

    import android.graphics.Bitmap;
    import android.graphics.Bitmap.CompressFormat;
    import android.graphics.BitmapFactory;

    public class ImageUtils {
        public static String getCompressedImagePath(String orgImagePath,
                String storeImagePath) {
            if (orgImagePath == null) {
                return null;
            }
            Bitmap bitmap = decodeSampledBitmapFromResource(orgImagePath, 100, 100);
            String absolutePath = "";
            FileOutputStream fos = null;
            try {

                fos = new FileOutputStream(storeImagePath);
                bitmap.compress(CompressFormat.PNG, 90, fos);
                fos.flush();
                absolutePath = storeImagePath;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NullPointerException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fos != null) {
                        fos.close();
                        fos = null;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return absolutePath;
        }

        public static Bitmap decodeSampledBitmapFromResource(String orgImagePath,
                int reqWidth, int reqHeight) {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(orgImagePath, options);
            options.inSampleSize = calculateInSampleSize(options, reqWidth,
                    reqHeight);
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeFile(orgImagePath, options);
        }

        public static int calculateInSampleSize(BitmapFactory.Options options,
                int reqWidth, int reqHeight) {
            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;
        }

      }