Reputation: 33
I have two android devices. CameraSource works differently for them. For one device the orientation of photo is correct without rotate after saving. But for another device saved the photo with wrong orientation.
I've created CameraSource:
source = new CameraSource.Builder(context, detector)
.setRequestedPreviewSize(640, 480)
.setFacing(cameraId)
.setRequestedFps(30.0f)
.setAutoFocusEnabled(true)
.build();
I've created button with action:
source.takePicture(null, new CameraSource.PictureCallback() {
@Override
public void onPictureTaken(byte[] bytes) {
File folder = PhotoUtils.getGalleryFolder();
writeFileIntoDevice(bytes, folder.getAbsolutePath());
}
});
private String writeFileIntoDevice(byte[] data, String path) {
Bitmap orignalImage = BitmapFactory.decodeByteArray(data, 0, data.length);
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd_hhmmss");
String fileName = formatter.format(new Date());
File file = new File(path, fileName + ".jpg");
try (FileOutputStream stream = new FileOutputStream(file)) {
orignalImage.compress(Bitmap.CompressFormat.JPEG, 80, stream);
Log.i(PhotoCreator.class.getName(), "photo was saved to " + path);
} catch (Exception e) {
Log.e(PhotoCreator.class.getName(), "can't save photo", e);
}
return file.getAbsolutePath();
}
Orientation of photos is different on the devices and I tried to rotate bitmap after save but without success.
I tried this: Controlling the camera to take pictures in portrait doesn't rotate the final images
And this: Android camera resulted image should be rotated after the capture?
How to rotate image correctly for all devices?
Upvotes: 3
Views: 1156
Reputation: 628
With two different devices, I am getting two different results. I am just starting the camera. With one device, I get the video preview properly, with another device, I get the video preview which is rotated by 90 degrees clockwise. My app is in landscape on an Android tablet.
I did not find any solution to fix this camera rotation issue.
cameraView = (SurfaceView)findViewById(R.id.cameraView);
barcodeDetector = new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.QR_CODE)
.build();
cameraSource = new CameraSource
.Builder(this, barcodeDetector)
.setAutoFocusEnabled(true)
.setRequestedFps(60)
.setRequestedPreviewSize(640, 480)
.setFacing(CameraSource.CAMERA_FACING_FRONT)
.build();
cameraSource.start(holder);
Upvotes: 1