I am creating an application in Android in which the user can only take a picture from the front camera in portrait mode. I managed to fix the camera image on the portrait, but when the image is taken, it rotates. The worst part is that the direction of rotation is different in different phones, in one phone it turns to the right, while in another it turns to the left.
Here are my code snippets
To ensure that activity is reproduced only in the portrait
<activity android:name=".MainActivity" android:label="@string/title_activity_main" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
Camera capture
@Override public void onResume() { super.onResume(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { Camera.CameraInfo info=new Camera.CameraInfo(); for (int i=0; i < Camera.getNumberOfCameras(); i++) { Camera.getCameraInfo(i, info); if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { camera=Camera.open(i); } } } if (camera == null) { camera=Camera.open(); } }
Camera rotation
@Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if(previewing){ camera.stopPreview(); previewing = false; } if (camera != null){ try { camera.setPreviewDisplay(surfaceHolder); camera.setDisplayOrientation(90); initPreview(width, height); startPreview(); previewing = true; } catch (IOException e) {
Photo taken
PictureCallback myPictureCallback_JPG = new PictureCallback(){ @Override public void onPictureTaken(byte[] data, Camera arg1) { // new SavePhotoTask().execute(data); Intent myIntent = new Intent(MainActivity.this, CropActivity.class); Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data .length); myIntent.putExtra("image", bitmap); MainActivity.this.startActivity(myIntent); }};
BMP dispatch always appears in rotating form, but not always with 90 degrees. It seems that Android 4.0 not only rotates, but also flips the image. Is there a good way to detect and make sure that I always get the right picture?