2

I develop app that save images to sd Card and all the pictures are upside i want to rotate them and save them in the rotate position i choose . i know how to rotate on my code but the image is not saved permanently. here is my code : //Rotate the picture

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);  
}

//Resize image

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);
    }
}

thanks for the helpers :)

Matan
  • 296
  • 5
  • 24
  • try this :saveBitmap = rotate(bitmap,40.00)... and saveBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); // Then save to file – KOTIOS Jun 27 '14 at 04:50
  • Look at my ans here :http://stackoverflow.com/questions/24128346/getting-rotation-from-exifinterface-always-returns-0/24219714#24219714 – Haresh Chhelana Jun 27 '14 at 05:03
  • I edited my question,How can i combine it with my resize method? – Matan Jun 27 '14 at 05:09

3 Answers3

1

You'll need to save the Bitmap back.

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) {}
}

Edit 1 :

Replace bmp.compress(Bitmap.CompressFormat.PNG, 90, out); with resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out); and set correct values for the directory path and the image name. If you want to replace the previous images, use the original path and image name.

Also, make sure you include the following permission.

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

Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
0

You can also try this one

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

Go through this link how to rotate a bitmap 90 degrees

Community
  • 1
  • 1
Jogendra Gouda
  • 405
  • 4
  • 17
0

The following code can help you compress and resize the bitmap.

Note: Create a String type variable with name of photoPath and store the photo url in it.

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);
    }

}
Shamshad Zaheer
  • 221
  • 3
  • 11