【问题标题】:Camera is rotating after taking photo in landscape, android在风景中拍照后相机正在旋转,android
【发布时间】:2013-06-26 13:21:19
【问题描述】:

我有一个捕获图像并将该图像放置在图像视图中的活动。但限制是我只需要将图像放置在纵向模式下,当我在横向模式下拍摄图像时会失败。因为在横向模式下捕获图像后图像会旋转。为了避免这种情况,我使用了

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,   
                    ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, 
                    ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);

我在清单文件中进行了更改,例如, 安卓:screenOrientation="人像" android:screenOrientation="reversePortrait"

但是上面的事情并没有解决问题,任何人都可以在这里帮助......

我的活动是,

    public class MainActivity extends Activity {

ImageView img1;
Button but;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);      
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    img1=(ImageView) findViewById(R.id.imageView1);
    but=(Button) findViewById(R.id.button1);
    but.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
             takephoto();               
        }
    });       

      }

      public void takephoto()
      {     
            Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        Uri pictureUri = Uri.fromFile(new 
        File(Environment.getExternalStorageDirectory()+"/img1.jpg"));
        camera.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);
        this.startActivityForResult(camera, 1);
        } 
     }


      @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data)
        {

    Bitmap 
   test=BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()
   +"/img1.jpg");
    img1.setImageBitmap(test);      
    }            
   }

我的清单文件是,

      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.a"
     android:versionCode="1"
      android:versionName="1.0" >

     <uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="17" />
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

     <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"                       
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    </manifest>

【问题讨论】:

    标签: android


    【解决方案1】:
    public Bitmap getImage1(String path) throws IOException
    {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        int srcWidth = options.outWidth;
        int srcHeight = options.outHeight;
        int[] newWH =  new int[2];
        newWH[0] = srcWidth/2;
        newWH[1] = (newWH[0]*srcHeight)/srcWidth;
    
        int inSampleSize = 1;
        while(srcWidth / 2 >= newWH[0]){
            srcWidth /= 2;
            srcHeight /= 2;
            inSampleSize *= 2;
        }
    
         options.inJustDecodeBounds = false;
        options.inDither = false;
        options.inSampleSize = inSampleSize;
        options.inScaled = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap sampledSrcBitmap = BitmapFactory.decodeFile(path,options);
        ExifInterface exif = new ExifInterface(path);
        String s=exif.getAttribute(ExifInterface.TAG_ORIENTATION);
        System.out.println("Orientation>>>>>>>>>>>>>>>>>>>>"+s);
        Matrix matrix = new Matrix();
        float rotation = rotationForImage(con, Uri.fromFile(new File(path)));
        if (rotation != 0f) {
            matrix.preRotate(rotation);
        }
    
        Bitmap pqr=Bitmap.createBitmap(
                sampledSrcBitmap, 0, 0, sampledSrcBitmap.getWidth(), sampledSrcBitmap.getHeight(), matrix, true);
    
    
        return pqr;
    }   
    
    
    public  float rotationForImage(Context context, Uri uri) {
        if (uri.getScheme().equals("content")) {
            String[] projection = { Images.ImageColumns.ORIENTATION };
            Cursor c = context.getContentResolver().query(
                    uri, projection, null, null, null);
            if (c.moveToFirst()) {
                return c.getInt(0);
            }
        } else if (uri.getScheme().equals("file")) {
            try {
                ExifInterface exif = new ExifInterface(uri.getPath());
                int rotation = (int)exifOrientationToDegrees(
                        exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                                ExifInterface.ORIENTATION_NORMAL));
                return rotation;
            } catch (IOException e) {
                e.printStackTrace();
            }catch (Exception e) {
                e.printStackTrace();
             }
    
        }
        return 0f;
    }
    
    private static float exifOrientationToDegrees(int exifOrientation) {
        if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
            return 90;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
            return 180;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
            return 270;
        }
        return 0;
    }
    

    将这些函数添加到您的活动中的 onActivity 结果

     @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data)
            {
    
        Bitmap 
       test=getImage1(Environment.getExternalStorageDirectory()+"/img1.jpg");
    
        img1.setImageBitmap(test);      
        }           
    

    【讨论】:

    • 感谢您的回答,请问我应该从哪里调用这个 getImage1() 方法
    • 我已经在我的回答中添加了它,它必须在 onActivityResult 中调用
    • 将它们添加到您的活动中并直接调用它
    • 这在模拟器中工作,现在我要在真实设备中检查它
    • 这种方法需要很多处理时间。以该方向拍摄照片的任何其他方式,可以为您提供用户拍摄图像的特定方向的图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多