13

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();
mix3d
  • 4,122
  • 2
  • 25
  • 47
androidavid
  • 1,258
  • 1
  • 11
  • 20

3 Answers3

26

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

}
Mayank Saini
  • 3,017
  • 24
  • 25
  • The problem that we have the programmers with the photos comes from the bitmap class! Always, you pass image for bitmap class this one rotates photo to 90 degrees. Then the solution is that always that we use a bitmap class and we need to show de photo we need to apply 'matrix.postRotate(90);' to create a rotated bitmap. – Merlí Escarpenter Pérez Jun 10 '15 at 15:14
4

Before you create your FileOutputStream you can create a new Bitmap from the original that has been transformed using a Matrix. To do that you would use this method:

createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)

Where the m defines a matrix that will transpose your original bitmap.

For an example on how to do this look at this question: Android: How to rotate a bitmap on a center point

Community
  • 1
  • 1
slayton
  • 20,123
  • 10
  • 60
  • 89
2
bitmap = RotateBitmap(bitmap, 90);

public static Bitmap RotateBitmap(Bitmap source, float angle)
{
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
Vignes
  • 390
  • 4
  • 11