-1
public void onClick(View v) {
    if (v == btn_camera) {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        Date date = new Date();
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");

        String newPicFile = "Miuzz"+ df.format(date) + ".jpg";
        String outPath = "/sdcard/" + newPicFile;
        File outFile = new File(outPath);

        mUri = Uri.fromFile(outFile);

        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);

        startActivityForResult(cameraIntent, 1);
    }
}

I use this to call camera function and then past to another activity for some uploading, but the orientation of photo is wrong. I know that it is impossible to change orientation after photo saving, others only try to display it as well by rotate and create new bitmap.

but i am going to upload a camera photo, how can i detect camera orientation and save image as that orientation, so that after i upload the image to the sever, the image will save as a right orientation?

Shirish Herwade
  • 11,461
  • 20
  • 72
  • 111
Kennett
  • 467
  • 2
  • 7
  • 17
  • Answer present here - http://stackoverflow.com/questions/14066038/why-image-captured-using-camera-intent-gets-rotated-on-some-devices-in-android – Shirish Herwade Mar 11 '16 at 13:56

1 Answers1

1

The code for rotating the image returned :

Bitmap captureBmp;
boolean image_tkn = false;
String pathToImage;
ImageUri = Uri.fromFile(image_file);
pathToImage = ImageUri.getPath();
file_path = pathToImage;
captureBmp = decodeFile(image_file);

File f = new File(file_path);
ExifInterface exif = new ExifInterface(f.getPath());
int orientation = exif.getAttributeInt(
        ExifInterface.TAG_ORIENTATION,
        ExifInterface.ORIENTATION_NORMAL);

int angle = 0;

if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
    angle = 90;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
    angle = 180;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
    angle = 270;
}

Matrix mat = new Matrix();
mat.postRotate(angle);
Bitmap bmp = decodeFile(f);
captureBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
        bmp.getHeight(), mat, true);
FileOutputStream fOut;
try {
    fOut = new FileOutputStream(f);
    captureBmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    fOut.flush();
    fOut.close();
} catch (Exception e) {
    e.printStackTrace();
}

The definition of the method decode file

private Bitmap decodeFile(File f) {
    Bitmap b = null;
    int IMAGE_MAX_SIZE = 300;
    try {
    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;

    FileInputStream fis = new FileInputStream(f);
    BitmapFactory.decodeStream(fis, null, o);
    fis.close();

    int scale = 1;
    if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
    scale = (int) Math.pow(
            2,
            (int) Math.round(Math.log(IMAGE_MAX_SIZE
                    / (double) Math.max(o.outHeight, o.outWidth))
                    / Math.log(0.5)));
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    fis = new FileInputStream(f);
    b = BitmapFactory.decodeStream(fis, null, o2);
    fis.close();
    } catch (IOException e) {
    }
    return b;
} 
Delon
  • 741
  • 1
  • 6
  • 14