I am building an android application that uses the camera capabilities. The issue I am having is the image data (byte[]) that I get from the front camera comes back upside down on my samsung s7 and nexus phone. It appears correct in the preview, but after I save the data as is and then display the images in a gallery they are all upside down. I know I can just flip the image data before saving but I have tested the code on a blu C 5.0 HD running 4.4 (kitkat) and the image data on that phone comes in oriented the right way. So alwyas flipping the image will cause the error on other devices. I am told the issue is because when the new samsung and nexus phones are built, the front facing cameras are built upside down to save room. I am not sure if this is correct,but if so, if I flip all images it will mess up the phones that have the correct orientation of the camera. So is there a way to detect the orientation of the image data before saving the image?
Here is the code I am using:
mCamera.takePicture(null, null, mPicture);
Callback:
private final Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
processCameraCallback(data);
}
};
Processing Data:
public void processCameraCallback(byte[] data) {
confirmPhoto(true);
//Make a new empty picture file
try {
pictureFile = Utils.createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Log.e(TAG, "Failed to create photo file: " + ex.toString());
confirmPhoto(false);
return;
}
//Write the file to the storage
try {
FileOutputStream fos;
if (pictureFile != null) {
fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
}
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
confirmPhoto(false);
return;
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
confirmPhoto(false);
return;
}
}