Android 设置安卓摄像头的方向从意向动作\图像\捕获开始

Android 设置安卓摄像头的方向从意向动作\图像\捕获开始,android,exif,android-camera-intent,Android,Exif,Android Camera Intent,我正在安卓系统的一个应用程序中工作,该应用程序使用摄像头拍照。启动摄像头时,我使用的是意图动作图像捕获,如下所示: Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File image=new File(Environment.getExternalStorageDirectory(),"PhotoContest.jpg"); camera.putExtra(MediaStore.EXTRA

我正在安卓系统的一个应用程序中工作,该应用程序使用摄像头拍照。启动摄像头时,我使用的是
意图
动作图像捕获
,如下所示:

Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File image=new File(Environment.getExternalStorageDirectory(),"PhotoContest.jpg");
        camera.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(image));
        imageUri=Uri.fromFile(image);
        startActivityForResult(camera,1);

public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode){
       case 1:
            if (resultCode == Activity.RESULT_OK) {
                  selectedImage = imageUri;
                  getContentResolver().notifyChange(selectedImage, null);
                  image= (ImageView) findViewById(R.id.imageview);
                  ContentResolver cr = getContentResolver();
                  Bitmap bitmap;
                  try {
                       bitmap = android.provider.MediaStore.Images.Media
                       .getBitmap(cr, selectedImage);
                       image.setImageBitmap(bitmap);
                       Toast.makeText(this, selectedImage.toString(),
                              Toast.LENGTH_LONG).show();
                  } catch (Exception e) {
                      Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                              .show();
                      Log.e("Camera", e.toString());
                  }
                 }
             else 

         if(resultCode == Activity.RESULT_CANCELED) {
                    Toast.makeText(EditPhoto.this, "Picture could not be taken.", Toast.LENGTH_SHORT).show();
                }
       }
}
问题是,所有拍摄的照片都以90度水平对齐旋转

我还将其放入我的清单文件:

 <activity android:name=".EditPhoto">
    android:screenOrientation="portrait"
    </activity>

android:screenOrientation=“肖像”
但还是没有结果!有人能帮我吗?

所以如果在

Activity.onActivityResult(data, request, result) {
 if (request == PHOTO_REQUEST && result == RESULT_OK) {
   ...
   Uri imageUri = ...
   File imageFile = new File(imageUri.toString());
   ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
   int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
   int rotate = 0;
   switch(orientation) {
     case ExifInterface.ORIENTATION_ROTATE_270:
         rotate-=90;
     case ExifInterface.ORIENTATION_ROTATE_180:
         rotate-=90;
     case ExifInterface.ORIENTATION_ROTATE_90:
         rotate-=90;
   }
   Canvas canvas = new Canvas(bitmap);
   canvas.rotate(rotate);
 }
这有帮助吗


再加上Greg的回答,这里有一个完整的“类别”来完成这项工作:

public static int neededRotation(File ff)
        {
        try
            {

            ExifInterface exif = new ExifInterface(ff.getAbsolutePath());
            int orientation = exif.getAttributeInt(
               ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

            if (orientation == ExifInterface.ORIENTATION_ROTATE_270)
                { return 270; }
            if (orientation == ExifInterface.ORIENTATION_ROTATE_180)
                { return 180; }
            if (orientation == ExifInterface.ORIENTATION_ROTATE_90)
                { return 90; }
            return 0;

            } catch (FileNotFoundException e)
            {
            e.printStackTrace();
            } catch (IOException e)
            {
            e.printStackTrace();
            }
        return 0;
        }
你或多或少会这样使用它

public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
    if (requestCode == REQUEST_IMAGE_CAPTURE) // && resultCode == RESULT_OK )
        {
        try
            {
            Bitmap cameraBmp = MediaStore.Images.Media.getBitmap(
                    State.mainActivity.getContentResolver(),
                    Uri.fromFile( Utils.tempFileForAnImage() )  );

            cameraBmp = ThumbnailUtils.extractThumbnail(cameraBmp, 320,320);
            // NOTE incredibly useful trick for cropping/resizing square
            // http://stackoverflow.com/a/17733530/294884

            Matrix m = new Matrix();
            m.postRotate( Utils.neededRotation(Utils.tempFileForAnImage()) );

            cameraBmp = Bitmap.createBitmap(cameraBmp,
                    0, 0, cameraBmp.getWidth(), cameraBmp.getHeight(),
                    m, true);

            yourImageView.setImageBitmap(cameraBmp);

            // to convert to bytes...
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            cameraBmp.compress(Bitmap.CompressFormat.JPEG, 75, baos);
            //or say cameraBmp.compress(Bitmap.CompressFormat.PNG, 0, baos);
            imageBytesRESULT = baos.toByteArray();

            } catch (FileNotFoundException e)
            {
            e.printStackTrace();
            } catch (IOException e)
            {
            e.printStackTrace();
            }

        return;
        }

    }

希望以后可以省去一些打字的时间。

上面的答案非常详尽,但我发现为了让每一个案例都有效,我必须做更多的工作,特别是如果你正在处理来自其他来源的图像,如图库或谷歌照片。这是我的确定方向的方法。我有一个实用程序类,它位于其中,因此我必须传入活动才能使用managedQuery(顺便说一句,不推荐使用managedQuery,所以请谨慎使用)。我必须使用两种方法的原因是,根据图像的来源,ExiFinInterface将无法工作。例如,如果我拍一张相机照片,Exif工作正常。但是,如果我也从Gallery或Google Drive中选择图像,Exif将不起作用,并且始终返回0。希望这对别人有帮助

public static int DetermineOrientation(Activity activity, Uri fileUri)
{
    int orientation = -1;
    int rotate = 0;
    try {

        ExifInterface exif = new ExifInterface(fileUri.getPath());
        orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        rotate = 0;
        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if(rotate == 0)
    {
        String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
        Cursor cur = activity.managedQuery(fileUri, orientationColumn, null, null, null);
        orientation = -1;
        if (cur != null && cur.moveToFirst()) {
            orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
        }

        if(orientation != -1)
        {
            rotate = orientation;
        }
    }

    return rotate;
}

您发布的代码用于在捕获图像后接收图像。我不认为你可以简单地把肖像画或风景画。这些更多地与视图的方向有关。但摄像机的安装和旋转方式要求用户旋转设备,以便在他们看来“正常”的情况下观察世界。因此,即使你说肖像画,图像也可以以另一种方式投影。在SDK的更高版本中,相机上有一个setRotation(使用它必须在旧版本上修改参数)。图像中可能有EXIF标题来告诉您方向。那么解决方案是什么呢!?如果没有您使用的捕获代码,很难判断。你能发布吗?我没有使用任何捕获代码…这是我在相机上使用的所有代码…你能给我一个完整的捕获代码和一起接收图像的代码示例吗?谢谢,不,这是一个很大的代码。我认为您实际上实现了该接口的另一端(侦听该意图的一端)。那么什么是编辑照片呢?它只是在纵向模式下显示横向图像吗?我不认为屏幕的方向会如此重要。这是什么装置?我自己造了照相机,格雷格。无论如何谢谢你。我不羡慕你。:)很高兴听到现在一切都畅通了。我想我会发布这个来完成它;我打赌位图是空的;)