0

我正在根据链接调整图像大小。
图像大小为 3264x2448,计算样本大小后现在为 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。

4

1 回答 1

1

好吧,我使用了您的代码并在我的示例应用程序中使用了它,它工作正常。所以你运行这个应用程序并找出你还缺少什么

活动-ImageScaleTestActivity

    包 com.myTutorial;

    导入java.io.File;

    导入 com.myTutorial.utils.ImageUtils;

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

    公共类 ImageScaleTestActivity 扩展 Activity {
        最终字符串 SRC_IMAGE = "/sdcard/Gallary/2.png";
        最终字符串 CONVERTED_IMAGE = "/sdcard/Gallary/Converted/1.png";
        布尔 isConvertedImage;
        /** 在第一次创建活动时调用。*/
        @覆盖
        公共无效 onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            设置内容视图(R.layout.main);

            按钮按钮 = (Button) findViewById(R.id.button1);
            button.setOnClickListener(new OnClickListener() {
                公共无效 onClick(查看 arg0){
                    isConvertedImage= !isConvertedImage;
                    if(isConvertedImage){
                        setImage(CONVERTED_IMAGE);
                    }别的{
                        设置图像(SRC_IMAGE);
                    }
                }
            });
            设置图像(SRC_IMAGE);
            ImageUtils.getCompressedImagePath(SRC_IMAGE, CONVERTED_IMAGE);
        }

        公共无效 setImage(字符串图像路径){
            文件 imgFile = new File(imagePath);
            如果(imgFile.exists()){

                位图 myBitmap = BitmapFactory.decodeFile(imgFile
                        .getAbsolutePath());

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

            }

        }
    }

XML-main.xml

    <?xml 版本="1.0" 编码="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        机器人:重力=“中心”
        安卓:方向=“垂直”>

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

        <按钮
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="显示转换后的图片" />

    </线性布局>

图像

    包 com.myTutorial.utils;

    导入 java.io.FileNotFoundException;
    导入 java.io.FileOutputStream;
    导入 java.io.IOException;

    导入android.graphics.Bitmap;
    导入android.graphics.Bitmap.CompressFormat;
    导入android.graphics.BitmapFactory;

    公共类 ImageUtils {
        公共静态字符串 getCompressedImagePath(字符串 orgImagePath,
                字符串存储图像路径){
            如果(orgImagePath == null){
                返回空值;
            }
            位图位图 = decodeSampledBitmapFromResource(orgImagePath, 100, 100);
            字符串绝对路径 = "";
            文件输出流 fos = null;
            尝试 {

                fos = new FileOutputStream(storeImagePath);
                bitmap.compress(CompressFormat.PNG, 90, fos);
                fos.flush();
                absolutePath = storeImagePath;
            } 捕捉(FileNotFoundException e){
                e.printStackTrace();
            } 捕捉(IOException e){
                e.printStackTrace();
            } 捕捉(NullPointerException e){
                e.printStackTrace();
            } 最后 {
                尝试 {
                    如果(fos!= null){
                        fos.close();
                        fos = null;
                    }
                } 捕捉(异常 e){
                    e.printStackTrace();
                }
            }
            返回绝对路径;
        }

        公共静态位图 decodeSampledBitmapFromResource(String orgImagePath,
                int reqWidth, int reqHeight) {
            最终 BitmapFactory.Options 选项 = 新 BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(orgImagePath, options);
            options.inSampleSize = calculateInSampleSize(选项,reqWidth,
                    要求高度);
            options.inJustDecodeBounds = false;
            返回 BitmapFactory.decodeFile(orgImagePath, options);
        }

        公共静态 int calculateInSampleSize(BitmapFactory.Options 选项,
                int reqWidth, int reqHeight) {
            最终 int 高度 = options.outHeight;
            最终 int 宽度 = options.outWidth;

            int inSampleSize = 1;
            if (height > reqHeight || width > reqWidth) {
                如果(宽度>高度){
                    inSampleSize = Math.round((float) height / (float) reqHeight);
                } 别的 {
                    inSampleSize = Math.round((float) width / (float) reqWidth);
                }
            }
            返回样本大小;
        }

      }

于 2012-06-19T11:37:40.837 回答