简体   繁体   English

如何将Android默认相机应用设置为仅纵向

[英]How to set Android default camera app for portrait orientation only

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. 在我的android应用程序中,我为此使用Android default camera捕获照片,下面使用了以下代码。捕获照片后,我已将Image显示为全屏模式下的另一个活动。如果以portrait模式捕获了图像,则可以设置整个图像进入全屏模式。 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. 显示完图像后,它可以执行我的操作。但是,如果我从landscape捕获照片,则所拍摄的图像将在屏幕上拉伸。 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 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 全屏拍摄的风景照片 在此处输入图片说明

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. 您的ImageView或其父级可能配置错误。 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. 如果要保持照片的长宽比, 并且希望ImageView填充某个区域,则ImageView需要具有相同的长宽比。

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. 如果您真的只想以人像模式拍照,则除了解决上面提到的问题之外,您还需要自己使用android.hardware.Camera ,而不是启动第三方相机应用程序。

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM