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

时间:2014-06-27 04:46:25

标签: 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);
    }
}

感谢帮助者:)

4 个答案:

答案 0 :(得分:4)

尝试以下代码。

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");    
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete (); 
try {
       finalBitmap = rotate(bmp,50);
       FileOutputStream out = new FileOutputStream(file);
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
       out.flush();
       out.close();

} catch (Exception e) {
       e.printStackTrace();
}

不要忘记从permission

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

答案 1 :(得分:1)

您需要保存位图。

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"/>

答案 2 :(得分:0)

您也可以试试这个

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

浏览此链接     how to rotate a bitmap 90 degrees

答案 3 :(得分:0)

以下代码可以帮助您压缩和调整位图大小。

注意: 创建一个名为photoPath的String类型变量,并将照片网址存储在其中。

public void compressImage(){
    Log.i("compressPhoto", "Compress and resize photo started.");

    // Getting Image
    InputStream in = null;
    try {
        in = new FileInputStream(photoPath);
    } catch (FileNotFoundException e) {
        Log.e("TAG","originalFilePath is not valid", e);
    }

    BitmapFactory.Options options = new BitmapFactory.Options();
    Bitmap bitmap = BitmapFactory.decodeStream(in, null, options);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap = bitmap.createScaledBitmap(bitmap,(int)(bitmap.getWidth()*0.2), (int)(bitmap.getHeight()*0.2), true);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 50, stream);

    byte[] byteArray = stream.toByteArray();

    // Storing Back
    FileOutputStream outStream = null;
    try {
        outStream = new FileOutputStream(photoPath);
        outStream.write(byteArray);
        outStream.close();
    } catch (Exception e) {
        Log.e("TAG","could not save", e);
    }

}