照片旋转90度,而捕获一些手机照片、手机

2023-09-12 10:21:26 作者:心里的痛楚她永远不懂

照片旋转90度,而从相机的​​其它移动设备的工作正常,三星手机休息捕捉。请帮我的。

 意图cameraIntent =新的意图(android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);
startActivityForResult(cameraIntent,IMAGE_CAPTURE);

@覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
    super.onActivityResult(要求code,因此code,数据);
    尝试
    {
         如果(要求code == IMAGE_CAPTURE){
            如果(结果code == RESULT_OK){

                乌里contentUri = data.getData();
                如果(contentUri!= NULL)
                {
                    的String []凸出= {MediaStore.Images.Media.DATA};
                    光标光标= managedQuery(contentUri,凸出,NULL,NULL,NULL);
                    INT与Column_Index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    cursor.moveToFirst();
                    imageUri = Uri.parse(cursor.getString(Column_Index中));
                }

                。tempBitmap =(位图)data.getExtras()获得(数据);
                mainImageView.setImageBitmap(tempBitmap);
                isCaptureFromCamera = TRUE;
            }
        }
 

解决方案

这恰好是在早期版本的Andr​​oid的一个bug。

我解决了这个问题,刚刚起步的方向角,并相应地旋转位图。

 公共位图德codeFILE(字符串路径){//你可以在这里提供的文件路径
    INT方向;
    尝试 {
        如果(路径== NULL){
            返回null;
        }
        //德code图像尺寸
        BitmapFactory.Options O =新BitmapFactory.Options();
        o.inJustDe codeBounds = TRUE;
        //找到正确的比例值。它应该是2的幂。
        最终诠释REQUIRED_SIZE = 70;
        INT width_tmp = o.outWidth,height_tmp = o.outHeight;
        int标= 0;
        而(真){
            如果(width_tmp / 2'; REQUIRED_SIZE
                    || height_tmp / 2版; REQUIRED_SIZE)
                打破;
            width_tmp / = 2;
            height_tmp / = 2;
        规模++;
        }
        //德code与inSampleSize
        BitmapFactory.Options O2 =新BitmapFactory.Options();
        o2.inSampleSize =规模;
        位图BM = BitmapFactory.de codeFILE(路径,O2);
        点阵位图= BM;

        ExifInterface EXIF​​ =新ExifInterface(路径);

        方向= exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,1);

        Log.e(ExifInteface .........,旋转=+方向);

        //exif.setAttribute(ExifInterface.ORIENTATION_ROTATE_90,90);

        Log.e(方向,+方向);
        矩阵M =新的Matrix();

        如果((取向== ExifInterface.ORIENTATION_ROTATE_180)){
            m.postRotate(180);
            //m.postScale((float)bm.getWidth(),(浮动)bm.getHeight());
            //如果(米。preRotate(90)){
            Log.e(定向,+方向);
            位图= Bitmap.createBitmap(宽多重峰,0,0,bm.getWidth(),bm.getHeight()中,m,真);
            返回的位图;
        }否则,如果(定向== ExifInterface.ORIENTATION_ROTATE_90){
            m.postRotate(90);
            Log.e(定向,+方向);
            位图= Bitmap.createBitmap(宽多重峰,0,0,bm.getWidth(),bm.getHeight()中,m,真);
            返回的位图;
        }
        否则,如果(定向== ExifInterface.ORIENTATION_ROTATE_270){
            m.postRotate(270);
            Log.e(定向,+方向);
            位图= Bitmap.createBitmap(宽多重峰,0,0,bm.getWidth(),bm.getHeight()中,m,真);
            返回的位图;
        }
        返回的位图;
    }赶上(例外五){
        返回null;
    }
}
 
苹果5手机相册里的图片如何旋转

Photo is rotating 90 degree while capturing from camera in samsung mobile rest of other mobiles its working fine. Please help me for this.

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, IMAGE_CAPTURE); 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) {     
    super.onActivityResult(requestCode, resultCode, data);    
    try
    {
         if (requestCode == IMAGE_CAPTURE) {
            if (resultCode == RESULT_OK){

                Uri contentUri = data.getData();
                if(contentUri!=null)
                {
                    String[] proj = { MediaStore.Images.Media.DATA };         
                    Cursor cursor = managedQuery(contentUri, proj, null, null, null);         
                    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);         
                    cursor.moveToFirst();         
                    imageUri = Uri.parse(cursor.getString(column_index));
                }

                tempBitmap = (Bitmap) data.getExtras().get("data"); 
                mainImageView.setImageBitmap(tempBitmap);
                isCaptureFromCamera = true;
            }
        }

解决方案

This happens to be a bug in earlier versions of Android.

I solved this issue just getting the orientation angle and rotate the bitmap accordingly.

public  Bitmap decodeFile(String path) {//you can provide file path here 
    int orientation;
    try {
        if (path == null) {
            return null;
        }
        // decode image size 
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        // Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE = 70;
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 0;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE
                    || height_tmp / 2 < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
        scale++;
        }
        // decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        Bitmap bm = BitmapFactory.decodeFile(path, o2);
        Bitmap bitmap = bm;

        ExifInterface exif = new ExifInterface(path);

        orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

        Log.e("ExifInteface .........", "rotation ="+orientation);

        //exif.setAttribute(ExifInterface.ORIENTATION_ROTATE_90, 90);

        Log.e("orientation", "" + orientation);
        Matrix m = new Matrix();

        if ((orientation == ExifInterface.ORIENTATION_ROTATE_180)) {
            m.postRotate(180);
            //m.postScale((float) bm.getWidth(), (float) bm.getHeight());
            // if(m.preRotate(90)){
            Log.e("in orientation", "" + orientation);
            bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), m, true);
            return bitmap;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            m.postRotate(90); 
            Log.e("in orientation", "" + orientation);
            bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), m, true);
            return bitmap;
        }
        else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            m.postRotate(270);
            Log.e("in orientation", "" + orientation);
            bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), m, true);
            return bitmap;
        } 
        return bitmap;
    } catch (Exception e) {
        return null;
    }
}