Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Rotate Picture before saving

I just finished my camera activity and it's wonderfully saving the data. What I do after the picture is taken:

protected void savePictureData() {
    try {
        FileOutputStream fs = new FileOutputStream(this.photo);
        fs.write(this.lastCamData);
        fs.close(); //okay, wonderful! file is just written to the sdcard

        //---------------------
        //---------------------
        //TODO in here: dont save just the file but ROTATE the image and then save it!
        //---------------------
        //---------------------


        Intent data = new Intent(); //just a simple intent returning some data...
        data.putExtra("picture_name", this.fname);
        data.putExtra("byte_data", this.lastCamData);
        this.setResult(SAVED_TOOK_PICTURE, data);
        this.finish(); 
    } catch (IOException e) {
        e.printStackTrace();
        this.IOError();
    }

}

What I want to is already as comment given in the code above. I dont want the image just to be saved to file but to be rotated and then saved! Thanks!

//EDIT: What I am currently up to (Works but still runs into memory issues with large images)

byte[] pictureBytes;
Bitmap thePicture = BitmapFactory.decodeByteArray(this.lastCamData, 0, this.lastCamData.length);
Matrix m = new Matrix();
m.postRotate(90);
thePicture = Bitmap.createBitmap(thePicture, 0, 0, thePicture.getWidth(), thePicture.getHeight(), m, true);

ByteArrayOutputStream bos = new ByteArrayOutputStream();
thePicture.compress(CompressFormat.JPEG, 100, bos);
pictureBytes = bos.toByteArray();

FileOutputStream fs = new FileOutputStream(this.photo);
fs.write(pictureBytes);
fs.close();
Intent data = new Intent();
data.putExtra("picture_name", this.fname);
data.putExtra("byte_data", pictureBytes);
this.setResult(SAVED_TOOK_PICTURE, data);
this.finish();
like image 949
androidavid Avatar asked Mar 07 '12 17:03

androidavid


People also ask

How do I save an image after rotating it?

1) Click on floppy disc icon that appears below Picture option or press Ctrl+S after making the rotation. 2) While closing the Picture Manager after the rotation, click on Save, when you receive the prompt' There are changes that have not been saved'.


1 Answers

Read the path from sd card and paste the following code...It'll Replace the existing photo after rotating it..

Note: Exif doesn't work on most of the devices, it returns incorrect data so it's good to hard code the rotation before saving to any degree you want to, Just change the angle value in postRotate.

    String photopath = tempphoto.getPath().toString();
    Bitmap bmp = BitmapFactory.decodeFile(photopath);

    Matrix matrix = new Matrix();
    matrix.postRotate(90);
    bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);

    FileOutputStream fOut;
    try {
        fOut = new FileOutputStream(tempphoto);
        bmp.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
        fOut.flush();
        fOut.close();

    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
like image 70
Mayank Saini Avatar answered Sep 21 '22 20:09

Mayank Saini