Android-采取从前置摄像头的画面旋转照片 [英] Android- Taking a picture from front facing camera rotates the photo

查看:251
本文介绍了Android-采取从前置摄像头的画面旋转照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造了一个Android的应用程序中,用户将能够利用从前置摄像头仅在纵向模式下的画面。我已经实现了固定摄影机视图的画像,但是当拍摄照片时,它的出现旋转。最糟糕的是,旋转的方向是不同的手机不同,在一个电话就向右旋转,而在另一个是向左旋转。

I am creating an app in android in which user will be able to take a picture from front camera in portrait mode only. I have achieved fixing the camera view to portrait but when a picture is taken it appears rotated. The worst thing is that the direction of rotation is different in different phones, in one phone it is right rotated whereas in another it is left rotated.

下面是我的code片段

Here is my code snippets

要确保活动在纵向只播放

To make sure activity plays in portrait only

 <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" 
             android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

要获取相机

   @Override
  public void onResume() {
    super.onResume();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
      Camera.CameraInfo info=new Camera.CameraInfo();

      for (int i=0; i < Camera.getNumberOfCameras(); i++) {
        Camera.getCameraInfo(i, info);

        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
          camera=Camera.open(i);
        }
      }
    }

    if (camera == null) {
      camera=Camera.open();
    }
  }

要旋转摄像头

    @Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    if(previewing){
        camera.stopPreview();
        previewing = false;
    }

    if (camera != null){
        try {
            camera.setPreviewDisplay(surfaceHolder);
            camera.setDisplayOrientation(90);
            initPreview(width, height);
            startPreview();

            previewing = true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

照片拍摄

     PictureCallback myPictureCallback_JPG = new PictureCallback(){

    @Override

     public void onPictureTaken(byte[] data, Camera arg1) {
         // new SavePhotoTask().execute(data);
        Intent myIntent = new Intent(MainActivity.this, CropActivity.class);
        Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data .length);
        myIntent.putExtra("image", bitmap);
        MainActivity.this.startActivity(myIntent);

     }};

的BMP发送总是出现在旋转的形式,但不总是90度。它看起来像的Andr​​oid 4.0不仅会旋转,但翻转映像。是否有检测,并确保我总是得到正确的图像的好方法?

The BMP send is always appearing in rotated form, but not always with 90 degree. It looks like android 4.0 not only rotates but flips the image as well. Is there a good way to detect and make sure I always get correct picture?

推荐答案

镜和翻转的问题是想通了,要具体到Android 4.0+,所以这个工作

The issue with mirror and flip was figured out to be specific to android 4.0+, So this worked

Matrix rotateRight = new Matrix();
rotateRight.preRotate(90);

if(android.os.Build.VERSION.SDK_INT>13 && CameraActivity.frontCamera)
{
    float[] mirrorY = { -1, 0, 0, 0, 1, 0, 0, 0, 1};
    rotateRight = new Matrix();
    Matrix matrixMirrorY = new Matrix();
    matrixMirrorY.setValues(mirrorY);

    rotateRight.postConcat(matrixMirrorY);

    rotateRight.preRotate(270);

}

final Bitmap rImg= Bitmap.createBitmap(img, 0, 0,
                img.getWidth(), img.getHeight(), rotateRight, true);

这篇关于Android-采取从前置摄像头的画面旋转照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆