Android 图像大小调整问题

Android 图像大小调整问题,android,bitmap,Android,Bitmap,我正在根据链接调整图像的大小图像大小为3264x2448,计算后样本大小现在为24,但图像旋转90度(左) reqWidth) { if (width > height) { inSampleSize = Math.round((float) height / (float) reqHeight); } else { inSampleSize = Mat

我正在根据链接调整图像的大小
图像大小为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方法已从链接中复制,并将压缩图像设置为InoImageView。

我使用了您的代码,并在示例应用程序中使用了该代码,效果良好。因此,您运行此应用程序并找出您还缺少什么

活动图像尺度稳定性

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); } } } 包com.myTutororial; 导入java.io.File; 导入com.myTutororial.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扩展活动{ 最后一个字符串SRC_IMAGE=“/sdcard/Gallary/2.png”; 最后一个字符串CONVERTED_IMAGE=“/sdcard/Gallary/CONVERTED/1.png”; 布尔变换图像; /**在首次创建活动时调用*/ @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); 按钮按钮=(按钮)findViewById(R.id.button1); setOnClickListener(新的OnClickListener(){ 公共void onClick(视图arg0){ isConvertedImage=!isConvertedImage; 如果(isConvertedImage){ setImage(转换的图像); }否则{ setImage(SRC_图像); } } }); setImage(SRC_图像); getCompressedImagePath(SRC_图像,转换的_图像); } public void setImage(字符串imagePath){ File imgFile=新文件(imagePath); if(imgFile.exists()){ 位图myBitmap=BitmapFactory.decodeFile(imgFile .getAbsolutePath()); ImageView myImage=(ImageView)findViewById(R.id.imageView1); 设置图像位图(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; } } 包com.myTutororial.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){ 返回null; } 位图位图=decodeSampledBitmapFromResource(orgImagePath,100100); 字符串绝对路径=”; FileOutputStream=null; 试一试{ fos=新文件输出流(storeImagePath); compress(CompressFormat.PNG,90,fos); fos.flush(); 绝对路径=存储图像路径; }catch(filenotfounde异常){ e、 printStackTrace(); }捕获(IOE异常){ e、 printStackTrace(); }捕获(NullPointerException e){ e、 printStackTrace(); }最后{ 试一试{ 如果(fos!=null){ fos.close(); fos=null; } }捕获(例外e){ e、 printStackTrace(); } } 返回绝对路径; } 公共静态位图解码SampledBitMapFromResource(字符串orgImagePath, 输入要求宽度,输入要求高度){ final BitmapFactory.Options=new BitmapFactory.Options(); options.inJustDecodeBounds=true; 解码文件(orgImagePath,选项); options.inSampleSize=计算样本大小(选项、要求宽度、, (高度); options.inJustDecodeBounds=false; 返回BitmapFactory.decodeFile(orgImagePath,选项); } 公共静态int-calculateInSampleSize(BitmapFactory.Options、, 输入要求宽度,输入要求高度){ 最终内部高度=options.outHeight; 最终整数宽度=options.outWidth; int inSampleSize=1; 如果(高度>要求高度| |宽度>要求宽度){ 如果(宽度>高度){ inSampleSize=数学圆((浮动)高度/(浮动)要求高度); }否则{ inSampleSize=数学圆((浮动)宽度/(浮动)宽度); } } 返回样本大小; } }
请张贴您正在使用的代码。