android - 旋转后如何保存图像位图?

标签 android bitmap

<分区>

我开发了将图像保存到 SD 卡的应用程序,并且所有图片都在上面我想旋转它们并将它们保存在我选择的旋转位置。 我知道如何旋转我的代码,但图像不会永久保存。 这是我的代码: //旋转图片

public static Bitmap rotate(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);

    return Bitmap.createBitmap(source, 0, 0, source.getWidth(),source.getHeight(), matrix, false);  
}

//调整图片大小

public void resizeImage(String path , int Wdist,int Hdist){
    try
    {
        int inWidth = 0;
        int inHeight = 0;


        InputStream in = new FileInputStream(path);

        // decode image size (decode metadata only, not the whole image)
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true; 
        BitmapFactory.decodeStream(in, null, options);
        in.close();
        in = null;

        // save width and height
        inWidth = options.outWidth;
        inHeight = options.outHeight;

        // decode full image pre-resized
        in = new FileInputStream(path);
        options = new BitmapFactory.Options();
        // calc rought re-size (this is no exact resize)
        options.inSampleSize = Math.max(inWidth/Wdist, inHeight/Hdist);
        // decode full image
        Bitmap roughBitmap = BitmapFactory.decodeStream(in, null, options);

        // calc exact destination size
        Matrix m = new Matrix();

        RectF inRect = new RectF(0, 0, roughBitmap.getWidth(), roughBitmap.getHeight());
        RectF outRect = new RectF(0, 0, Wdist, Hdist);
        m.setRectToRect(inRect, outRect, Matrix.ScaleToFit.CENTER);
        float[] values = new float[9];
        m.getValues(values);

        // resize bitmap
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(roughBitmap, (int) (roughBitmap.getWidth() * values[0]), (int) (roughBitmap.getHeight() * values[4]), true);

        // save image
        try
        {
            FileOutputStream out = new FileOutputStream(path);
            resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
        }
        catch (Exception e)
        {
            Log.e("Image", e.getMessage(), e);
        }
    }
    catch (IOException e)
    {
        Log.e("Image", e.getMessage(), e);
    }
}

感谢帮助者:)

最佳答案

您需要将位图保存回来。

try {
       File dir = new File("path/to/directory");
       if(!dir.exists())
           dir.mkdirs();
       File file = new File(dir, "original_img_name.png");
       FileOutputStream out;
       out = new FileOutputStream(file);
       bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
    e.printStackTrace();
} finally {
       try{
           out.close();
       } catch(Throwable ignore) {}
}

编辑 1:

替换 bmp.compress(Bitmap.CompressFormat.PNG, 90, out);resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);并为目录路径和图像名称设置正确的值。如果要替换以前的图片,请使用原始路径和图片名称。

此外,请确保您包含以下权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

关于android - 旋转后如何保存图像位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24444133/

相关文章:

android - 为什么调整大小会使我的图像看起来很奇怪?

vba - 如何使用 pastepecial 将图表作为位图粘贴到 vba 中的另一张纸上

android - 如何保存android配置布局

android - 在创建应用程序子类以与多个 Activity 共享数据时遇到问题

wpf - 我如何将简单的形状位图转换为矢量(几何)

android - 旋转图像时如何检测触摸了图像的哪一部分?

Android ContextMenu 如何设置TextColor 和背景颜色?

java - 如何像android一样动态地将密码属性设置为EditTextPreference :password ="true"?

java - 待处理 Intent 警报管理器不能调用两次

java 。运行和处理位图上每个像素的最快方法是什么?