如何设置相机的图像方向? [英] how to set camera Image orientation?

查看:192
本文介绍了如何设置相机的图像方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序添加了图片上传的功能,它工作正常,除了摄像头图像中的所有图像,每当我浏览摄像机图像的画廊和纵向的图像旋转90 degree..following是我的片断code。 。可谁能帮助我?我跟很多教程,但在kikat..but他们都很好地工作时一样的教程不ICS的工作,软糖等。

 公共类MainActivity延伸活动{
私人按钮浏览;
私人字符串selectedImagePath =;
私人ImageView的IMG;

私人TextView中的MessageText;

私有静态诠释SELECT_PICTURE = 1;
@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    IMG =(ImageView的)findViewById(R.id.imagevw);
    浏览=(按钮)findViewById(R.id.browseimg);
    =的MessageText(TextView中)findViewById(R.id.messageText);
    browse.setOnClickListener(新OnClickListener(){

        @覆盖
        公共无效的onClick(视图v){
            // TODO自动生成方法存根
             意向意图=新的意图();
                intent.setType(图像/ *);
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(意向,选择图片),SELECT_PICTURE);
        }
    });
}
 @覆盖
   公共无效onActivityResult(INT申请code,INT结果code,意图数据){
       如果(结果code == RESULT_OK){
           如果(要求code == SELECT_PICTURE){
               乌里selectedImageUri = data.getData();

               / *字符串文件路径= getRealPathFromURI(getActivity(),selectedImageUri);
               messageText.setText(文件路径);
               Picasso.with(getActivity())
                                      .load(新File(文件路径))
                                      .centerCrop()
                                      .resize(60,60)。走进(IMG); * /

               selectedImagePath = getPath(selectedImageUri);
               messageText.setText(selectedImagePath);
               的System.out.println(要求code);
               的System.out.println(映像路径:+ selectedImagePath);
               img.setImageURI(selectedImageUri);
           }
       }
 }
 @燮pressWarnings(德precation)
    公共字符串getPath(URI URI){
       的String []投影= {MediaStore.Images.Media.DATA};
       光标光标= managedQuery(URI,投影,NULL,NULL,NULL);
       INT与Column_Index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
       cursor.moveToFirst();
       返回cursor.getString(Column_Index中);
   }

   }
 

解决方案

只是包含这个code

 公共无效rotateImage(字符串文件)抛出IOException异常{

    BitmapFactory.Options边界=新BitmapFactory.Options();
    bounds.inJustDe codeBounds = TRUE;
    BitmapFactory.de codeFILE(文件范围);

    BitmapFactory.Options选择采用=新BitmapFactory.Options();
    位图BM = BitmapFactory.de codeFILE(文件,选择采用);

    INT rotationAngle = getCameraPhotoOrientation(getActivity(),Uri.fromFile(文件1),file1.toString());

    字模=新的Matrix();
    matrix.postRotate(rotationAngle,(浮动)bm.getWidth()/ 2,(浮动)bm.getHeight()/ 2);
    位图rotatedBitmap = Bitmap.createBitmap(宽多重峰,0,0,bounds.outWidth,bounds.outHeight,矩阵,真);
    FileOutputStream中FOS =新的FileOutputStream(文件);
    rotatedBitmap.com preSS(Bitmap.Com pressFormat.JPEG,100,FOS);
    fos.flush();
    fos.close();
}

公共静态INT getCameraPhotoOrientation(上下文的背景下,开放的我们imageUri,字符串的ImagePath){
    INT旋转= 0;
    尝试 {
        context.getContentResolver()有NotifyChange(imageUri,空)。
        文件镜像文件=新的文件(的ImagePath);
        ExifInterface EXIF​​ =新ExifInterface(imageFile.getAbsolutePath());
        INT方向= exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);
        开关(方向){
        案例ExifInterface.ORIENTATION_NORMAL:
            转动= 0;
        案例ExifInterface.ORIENTATION_ROTATE_270:
            旋转= 270;
            打破;
        案例ExifInterface.ORIENTATION_ROTATE_180:
            旋转= 180;
            打破;
        案例ExifInterface.ORIENTATION_ROTATE_90:
            旋转= 90;
            打破;
        }
    }赶上(例外五){
        e.printStackTrace();
    }
    返回旋转;
}
 

In my application I have added feature of image uploading,It works fine with all the Images except camera image,whenever I browse camera image from gallery and portrait image rotate in 90 degree..following is my snippet code..can anyone help me?I followed so many tutorials but all of them work well in kikat..but when same tutorial does not work with ics,jellybean etc..

public class MainActivity extends Activity {
private Button browse;
private String selectedImagePath="";
private ImageView img;

private TextView messageText;

private static int SELECT_PICTURE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    img = (ImageView)findViewById(R.id.imagevw);
    browse=(Button)findViewById(R.id.browseimg);
    messageText  = (TextView)findViewById(R.id.messageText);
    browse.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
        }
    });
}
 @Override
   public void onActivityResult(int requestCode, int resultCode, Intent data) {
       if (resultCode == RESULT_OK) {
           if (requestCode == SELECT_PICTURE) {
               Uri selectedImageUri = data.getData();

               /*String filePath = getRealPathFromURI(getActivity(), selectedImageUri );
               messageText.setText(filePath );
               Picasso.with(getActivity())
                                      .load(new File(filePath ))
                                      .centerCrop()
                                      .resize(60, 60).into( img);*/

               selectedImagePath = getPath(selectedImageUri);
               messageText.setText(selectedImagePath);
               System.out.println(requestCode);
               System.out.println("Image Path : " + selectedImagePath);
               img.setImageURI(selectedImageUri);
           }
       }
 }
 @SuppressWarnings("deprecation")
    public String getPath(Uri uri) {
       String[] projection = { MediaStore.Images.Media.DATA };
       Cursor cursor = managedQuery(uri, projection, null, null, null);
       int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
       cursor.moveToFirst();
       return cursor.getString(column_index);
   }

   }

解决方案

just include this code

public void rotateImage(String file) throws IOException{

    BitmapFactory.Options bounds = new BitmapFactory.Options();
    bounds.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(file, bounds);

    BitmapFactory.Options opts = new BitmapFactory.Options();
    Bitmap bm = BitmapFactory.decodeFile(file, opts);

    int rotationAngle = getCameraPhotoOrientation(getActivity(), Uri.fromFile(file1), file1.toString());

    Matrix matrix = new Matrix();
    matrix.postRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
    Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);
    FileOutputStream fos=new FileOutputStream(file);
    rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    fos.flush();
    fos.close();
}

public static int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){
    int rotate = 0;
    try {
        context.getContentResolver().notifyChange(imageUri, null);
        File imageFile = new File(imagePath);
        ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
        int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);
        switch (orientation) {
        case ExifInterface.ORIENTATION_NORMAL:
            rotate = 0;
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = 270;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rotate;
}

这篇关于如何设置相机的图像方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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