i've been struggling with the problem below, i found some tutorials and made a custom camera with a SurfaceView as a preview holder. I'm mainly concerned for the front Camera. Now when i take an image while the preview works fine the image is being saved with a wrong rotation in my SD card.
Below are the main two functions. Setup of the camera and the take picture.
*private void setUpCamera(Camera c) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
rotation = getWindowManager().getDefaultDisplay().getRotation();
int degree = 0;
switch (rotation) {
case Surface.ROTATION_0:
degree = 0;
break;
case Surface.ROTATION_90:
degree = 90;
break;
case Surface.ROTATION_180:
degree = 180;
break;
case Surface.ROTATION_270:
degree = 270;
break;
default:
break;
}
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
// frontFacing
rotation = (info.orientation + degree) % 360;
rotation = (360 - rotation) % 360;
} else {
// Back-facing
rotation = (info.orientation - degree + 360) % 360;
}
c.setDisplayOrientation(rotation);
Parameters params = c.getParameters();
List<String> focusModes = params.getSupportedFlashModes();
if (focusModes != null) {
if (focusModes
.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
params.setFlashMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
}
}
params.setRotation(rotation);
c.setParameters(params);
}*
private void takeImage() {
camera.takePicture(null, null, new PictureCallback() {
private File imageFile;
@Override
public void onPictureTaken(byte[] data, Camera camera) {
try {
// convert byte array into bitmap
Bitmap loadedImage = null;
loadedImage = BitmapFactory.decodeByteArray(data, 0,data.length);
// rotate Image
Matrix rotateMatrix = new Matrix();
rotateMatrix.postRotate(rotation);
loadedImage = Bitmap.createBitmap(loadedImage, 0, 0,loadedImage.getWidth(), loadedImage.getHeight(),rotateMatrix, false);
//create folder if it doesnt exists
String state = Environment.getExternalStorageState();
File folder = null;
if (state.contains(Environment.MEDIA_MOUNTED)) {
folder = new File(Environment
.getExternalStorageDirectory() + "/Squote");
} else {
folder = new File(Environment
.getExternalStorageDirectory() + "/Squote");
}
boolean success = true;
if (!folder.exists()) {
success = folder.mkdirs();
}
if (success) {
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
imageFile = new File(folder.getAbsolutePath()
+ File.separator
+ "IMG_"
+ timeStamp
+ ".jpg");
imageFile.createNewFile();
} else {
Toast.makeText(getBaseContext(), "Image Not saved",Toast.LENGTH_SHORT).show();
return;
}
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
// save image into gallery
loadedImage.compress(CompressFormat.JPEG, 100, ostream);
FileOutputStream fout = new FileOutputStream(imageFile);
fout.write(ostream.toByteArray());
fout.close();
ContentValues values = new ContentValues();
values.put(Images.Media.DATE_TAKEN,System.currentTimeMillis());
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.MediaColumns.DATA, imageFile.getAbsolutePath());
//release camera and return image
releaseCamera();
Intent intentMessage = new Intent();
intentMessage.putExtra("imageUri", imageFile.getAbsolutePath());
setResult(RESULT_OK, intentMessage);
finish();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
On the rotate image
part of the code if i put 270
it shows up correct BUT i'm having another issue there. The image is not the same as the preview but it's mirrored. I think also that a fixed 270 value is not a solution as it messes up the back camera or the portrait pictures.
Any help would be much appreciated.
Thank you
It's not an issue, it's how the front-facing camera works. Even if you take a picture with your phone's camera app it will be flipped when saved. You can implement a method to flip the image vertically if that is the result you are trying to get.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.