简体   繁体   English

在人像模式下拍摄的图像应在android中保留为横向模式

[英]image taken when in portrait mode should retain in landscape mode in android

I am creating an application in which on click on the action bar item, the camera activity starts, and after capturing and saving the pic, it displays it on the main activity. 我正在创建一个应用程序,在该应用程序中,单击操作栏项,将启动摄影机活动,并在捕获并保存图片后将其显示在主要活动上。

Now if i start the Camera from the MainActivity while in portrait orientation, and switch to landscape orientation in the Camera Activity, take the picture, and click on the Save button, it returns to the MainActivity, but image is not displayed, this is I guess because the main activity is getting reloaded, but I a not sure though. 现在,如果我以肖像方向从MainActivity启动相机,然后在Camera Activity中切换为横向,拍摄图片,然后单击“保存”按钮,它将返回MainActivity,但是没有显示图像,这就是我猜猜是因为主要活动正在重新加载,但是我不确定。

How do I have to retain the image even after the change in orientation. 即使改变方向,我如何也必须保留图像。

MainActivity.java MainActivity.java

public boolean onOptionsItemSelected(MenuItem item) {
     switch (item.getItemId()) {
     case R.id.menu_camera: 

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        // file creation for saving video
        Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);  
        if (fileUri != null) {
            targetFile = fileUri.getPath();

            // setting file image name
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);   

            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

            startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
        }

        return true;

onActivityResult() onActivityResult()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Image captured and saved to fileUri specified in the Intent
            ImageView imgPicture = (ImageView) findViewById(R.id.imgPicture);
            if (targetFile != null) {
                theFile = targetFile;
                imgPicture.setImageBitmap(BitmapFactory.decodeFile(theFile));
            }

        } else if (resultCode == RESULT_CANCELED) {

        } else {

            Toast.makeText(this, "Your picture could not be saved.", Toast.LENGTH_LONG).show();
        }
    }
}

Manifest.xml Manifest.xml

 <uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="17" />

<uses-feature
    android:name="android.hardware.camera"
    android:required="true" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.ActionBarTest.MainActivity"
        android:label="@string/app_name" 
        android:launchMode="singleTop">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

maybe you can use this to be able to know how the image saved and then make bitmap or whatever based on that 也许您可以使用它来知道如何保存图像,然后制作位图或基于该图像的任何东西

public static int getExifRotation(String imgPath) 
    {
        try 
        {
            ExifInterface exif = new ExifInterface(imgPath);
            String rotationAmount = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
            if (!TextUtils.isEmpty(rotationAmount)) 
            {
                int rotationParam = Integer.parseInt(rotationAmount);
                switch (rotationParam) 
                {
                    case ExifInterface.ORIENTATION_NORMAL:
                        return 0;
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        return 90;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        return 180;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        return 270;
                    default:
                        return 0;
                }
            } 
            else 
            {
                return 0;
            }
        }
        catch (Exception ex) 
        {
            return 0;
        }
    }

also check 还检查

if(bitmap.getWidth() > bitmap.getHeight()){}

that will let you know if the picture is landscape because width would be greater than height and use the bitmap constructor that takes matrix to rotate: 这将让您知道图片是否为横向,因为宽度将大于高度,并使用需要矩阵旋转的位图构造函数:

Matrix matrix = new Matrix();
matrix.postRotate(90); //or whatever

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

相关问题 如何检查照片是在Android的横向还是纵向模式下拍摄的? - How to check whether the photo was taken in landscape or portrait mode in Android? 纵向模式下的Android Landscape VideoView - Android Landscape VideoView in portrait mode 适用于Android应用程序的横向和纵向模式 - Landscape and portrait mode for android application 平板电脑Android中的横向和纵向模式 - Landscape and portrait mode in tablet Android Android 中的横向模式更改为纵向 - Landscape Mode change to portrait in Android 纵向模式图像被requestCameraImage保存为横向模式 - portrait mode image saved as landscape mode by requestCameraImage 如何设置几个片段是横向模式和休息应该是Android中的肖像? - How to set few fragment be landscape mode and rest should be portrait in android? 在android中从纵向模式切换到横向模式时如何避免更改图像和布局位置 - How to avoid changing of image and layout position when changing from portrait to landscape mode in android android:从纵向模式转到横向模式时按钮未居中 - android: button not centered when turned from portrait to landscape mode Android应用程序设计用于在恢复时在横向显示的纵向模式 - android app designed for portrait mode displayed in landscape when resuming
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM