1

I have a custom camera activity which is supposed to take pictures in portrait mode only, however in Samsung devices the pictures always turns out in landscape mode on my nexus device the picture is displaying correctly in portrait mode. Below is my method for setting the camera parameters and orientation. This initPreview is called in the SurfaceHolder.Callback surfaceChanged method.

 /** INITIALIZE THE PREVIEW PARAMTERS */
 private void initPreview(int width, int height) {
    if ( mCamera != null && previewHolder.getSurface() != null) {
        try {
            mCamera.setPreviewDisplay( previewHolder );
        }
        catch (Throwable t) {
            Log.e("PreviewDemo-surfaceCallback", "Exception in setPreviewDisplay()", t);
            Crouton.makeText( this, t.getMessage(), Style.ALERT ).show();
        }

        if (!cameraConfigured) {
            Camera.Parameters parameters = mCamera.getParameters();
            Camera.Size size = getBestPreviewSize(width, height, parameters);
            Camera.Size pictureSize = getSmallestPictureSize(parameters);

            if (size != null && pictureSize != null) {
                parameters.setPreviewSize( size.width, size.height );
                parameters.setPictureSize( pictureSize.width, pictureSize.height );
                parameters.setPictureFormat( ImageFormat.JPEG);

                parameters.set( "orientation", "portrait" );
                parameters.setRotation( 90 );
                mCamera.setDisplayOrientation( 90 );
                mCamera.setParameters( parameters );
                cameraConfigured = true;
            }
        }
    }
}
kabuto178
  • 3,129
  • 3
  • 40
  • 61

1 Answers1

0

background

Samsung is buggy in this regard. You can try this...

Get the orientation to isolate photo URL's from samsung where there is issue...

public static int getOrientation(Context context, Uri photoUri) {
    /* it's on the external media. */
    Cursor cursor = context.getContentResolver().query(photoUri,
            new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

    if (cursor.getCount() != 1) {
        return -1;
    }

    cursor.moveToFirst();
    return cursor.getInt(0);
}

Once you have that identified, do a bitmap rotate to correct the Samsung issue...

                            if( getOrientation(ctx, photoUrl) == 0 ||  getOrientation(ctx, photoUrl) == -1){
                                //not samsung issue so process as normal;
                            }else{  // bad samsung needs rotation

                                Bitmap lbit =   
                                BitmapFactory.decodeStream(
                                        getContentResolver().openInputStream(_photo_uri),null,options);

                                lbit = lbit.copy(Bitmap.Config.ARGB_8888, true);
                                Matrix matrix = new Matrix();
                                matrix.postRotate(intReturnFromGetOrientationCall);
                                lbit = Bitmap.createBitmap(lbit, 0, 0, lbit.getWidth(),
                                        lbit.getHeight(), matrix, true);
                                Picture.setBmp(lbit);
                            }
Community
  • 1
  • 1
Robert Rowntree
  • 6,230
  • 2
  • 24
  • 43
  • Thanks for your reply, however I do get a null point on this line ` if (cursor.getCount() != 1) {` – kabuto178 Sep 27 '14 at 03:06
  • Even the `Exif` information is all null, the orientation for all pictures taken always comes back as `0` – kabuto178 Sep 27 '14 at 17:03
  • getOrientation() doesn't use Exif. Its a diff way of dealing with the Samsung issue. There are 2 solutions talked about. Using Exif did not work for me either. the background link talks about Exif, but you should be aware that Exif did NOT solve lots of problems. Thus the use of the mediaCursor to determine if pic has been rotated by Samsung device. – Robert Rowntree Sep 27 '14 at 17:07
  • The media query to get the orientation of the image returns null, not sure if it will help but notice in my initial camera set up I set the rotation to be `90` – kabuto178 Sep 27 '14 at 17:12
  • http://stackoverflow.com/questions/8450539/images-taken-with-action-image-capture-always-returns-1-for-exifinterface-tag-or/8864367#8864367 – Robert Rowntree Sep 27 '14 at 17:43
  • Will take a look there – kabuto178 Sep 27 '14 at 17:52