【问题标题】:set orientation of android camera started with intent ACTION_IMAGE_CAPTURE [duplicate]以意图 ACTION_IMAGE_CAPTURE [重复] 开始设置 android 相机的方向
【发布时间】:2011-10-12 09:25:54
【问题描述】:

我正在使用相机拍摄照片的 android 应用程序。为了启动相机,我使用 intent ACTION_IMAGE_CAPTURE 像这样:

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>

但还是没有结果!那么谁能帮帮我???

【问题讨论】:

  • 您发布的代码用于在捕获图像后接收图像。我认为您不能简单地放置肖像或风景。这些更多地与视图的方向有关。但是相机可以安装和旋转这样一种方式,即它需要用户旋转设备才能以他们看起来“正常”的方式查看世界。因此,即使您说肖像,也可以以另一种方式投影图像。在 SDK 的更高版本中,相机上有一个 setRotation(使用必须破解旧版本的参数)。图片中可能有 EXIF 标头来告诉您方向。
  • 那么解决办法是什么!?
  • 如果没有您使用的捕获代码,就很难分辨。可以发一下吗?
  • 我没有使用任何捕获代码...这就是我用于相机的全部内容...您能给我一个完整的捕获代码示例和一起接收图像的代码吗??谢谢
  • 呃,不,那是大量的代码。我以为你实际上实现了这个接口的另一端(监听那个意图的那个)。那么什么是编辑照片?它是否仅以纵向模式显示横向图像?我认为屏幕方向不会那么重要。这是什么设备?

标签: android exif android-camera-intent


【解决方案1】:

http://developer.android.com/reference/android/media/ExifInterface.html

http://developer.android.com/reference/android/media/ExifInterface.html#TAG_ORIENTATION

所以如果在

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

这有帮助吗?


为了补充格雷格的好答案,这里有一个完整的“类别”来完成这项工作:

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

    }

希望它可以为将来的人节省一些打字时间。

【讨论】:

  • 我自己造了相机,Greg。谢谢你
  • 我不羡慕你。 :) 很高兴听到这一切现在都畅通无阻了。只是想我会发布这个以完成。
  • 在 Canvas 中给我 nullpointer exeption = new Canvas(bitmap);
  • 我敢打赌位图是空的 ;)
【解决方案2】:

上述答案非常详尽,但我发现我必须做更多工作才能使每个案例都能正常工作,尤其是在您处理来自其他来源(例如图库或 Google 照片)的图像时。这是我的确定方向方法。我有一个实用程序类,所以我必须传入 Activity 才能使用 managedQuery(顺便说一句,它已被弃用,因此请谨慎使用)。我必须使用两种方法的原因是,根据图像的来源,ExifInterface 将不起作用。例如,如果我拍一张相机照片,Exif 就可以正常工作。但是,如果我还从图库或 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多