设置Android摄像头的方向以意图ACTION_IMAGE_CAPTURE开始

时间:2011-07-25 07:57:17

标签: android exif android-camera-intent

我正在安卓的应用程序中使用相机来拍照。为了启动相机,我正在使用这样的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>

但仍然没有结果!所以任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:32)

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

这有帮助吗?


只是为了补充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;
        }

    }

希望将来可以为某人打字。

答案 1 :(得分:3)

上述答案非常彻底,但我发现为了让每个案例都有效,我必须多做一些工作,特别是如果您正在处理来自其他来源(如图库或Google相册)的图像。这是我的DetermineOrientation方法。我有一个实用程序类,所以我必须传入Activity才能使用managedQuery(不推荐使用btw,所以谨慎使用)。我必须使用两种方法的原因是,根据图像的来源,ExifInterface将无法工作。例如,如果我拍摄相机照片,Exif工作正常。但是,如果我还从图库或Google云端硬盘中选择图片,则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;
}