How to set Android default camera app for portrait

2019-03-03 23:44发布

问题:

In my android application I am capturing photo using Android default camera for this purpose I have used below code.after captured a photo I have shown that Image into another activity in full screen mode.if I captured image in portrait mode I can set the whole image into full screen mode. it showed good, after showed a image I can perform my operation.But if I captured a photo from landscape the taken image would set in screen with stretched. so I would like to capture photo using portrait mode only not in landscape. so how may I lock camera app only for portrait.

String fileName = "Camera_Example.jpg";                
    ContentValues values = new ContentValues();                
    values.put(MediaStore.Images.Media.TITLE, fileName);                
    values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");                
    imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);                       
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);   
    startActivityForResult(intent,CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 

AndroidManifest.xml

 <activity
     android:name=".TestActivity"
     android:screenOrientation="portrait"
     android:configChanges="orientation"
     android:theme="@android:style/Theme.NoTitleBar" >
     </activity>

portrait captured photo

portrait captured photo full screen

landscape captured photo

landscape captured photo full screen

回答1:

how may I lock camera app only for portrait

You don't. You did not write the camera app, as there are hundreds, perhaps thousands, of camera apps. It is up to the camera app and the user to handle orientation.

Besides:

  • on many devices, you will have the same problem in portrait mode

  • your landscape photo also appears to be stretched, though not as drastically

But if I captured a photo from landscape the taken image would set in screen with stretched

You have one, and perhaps two, problems:

  1. You are not taking into account the orientation of the photo, and so you are loading a landscape photo as if it were portrait.

  2. Your ImageView, or perhaps its parent, is probably misconfigured. If you want to maintain the aspect ratio of the photo, and you want that ImageView to fill some area, then the ImageView needs to have that same aspect ratio.

If you really want to only take photos in portrait mode, you would need to use android.hardware.Camera yourself, rather than launching a third-party camera app, in addition to addressing the problems I mention above.



回答2:

Try the following code to rotate the camera:

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    camera = Camera.open();
    camera.setDisplayOrientation(90); // camera  portrait oriantation
    try {
        camera.setPreviewDisplay(holder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}