图片使用相机的意图旋转90度 [英] Image rotate 90 degree using camera intent

查看:111
本文介绍了图片使用相机的意图旋转90度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我工作在一个Android应用程序,我需要用相机捕捉图像意图,并设置位图中的的ImageView 但这里是位90度旋转。我检查了计算器的多个线程像<一个href=\"http://stackoverflow.com/questions/13517200/photo-rotate-90-degree-while-capture-in-some-phones\">Photo旋转90度,而在捕捉一些手​​机的,但并没有为我工作。

当我执行这个 exif.getAttributeInt这里(ExifInterface.TAG_ORIENTATION,1); 那么它返回0 ORIENTATION_UNDEFINED 在我的的getImage 函数没有条件是令人满意的。

 意图cameraIntent =新意图(
        android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);
capturedPhotoName = System.currentTimeMillis的()+的.png;
文件照片=新的文件(Environment.getExternalStorageDirectory()
        capturedPhotoName);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(照片));
imageUri = Uri.fromFile(照片);
startActivityForResult(cameraIntent,CAMERA_INTENT_REQUEST);

的onActivityResult

 乌里selectedImage = imageUri;
。getContentResolver()有NotifyChange(selectedImage,NULL);
ContentResolver的CR = getContentResolver();
位图位图;
尝试{
    位= android.provider.MediaStore.Images.Media.getBitmap(CR,
            selectedImage);
    位图= Util.getImage(位图,selectedImage.toString());
    mPictureImageView.setImageBitmap(位图);
}赶上(例外五){
    Log.e(新发行活动,e.toString());
}/ **
 *获取图像方向
 *
 * @参数的ImagePath
 * @返回orietation角
 *引发IOException
 * /
公共静态位图的getImage(位图位图,字符串路径)抛出IOException
    矩阵M =新的Matrix();
    ExifInterface EXIF​​ =新ExifInterface(路径);
    INT方向= EXIF
            .getAttributeInt(ExifInterface.TAG_ORIENTATION,1);
    如果((取向== ExifInterface.ORIENTATION_ROTATE_180)){
        m.postRotate(180);
        位图= Bitmap.createBitmap(位图,0,0,bitmap.getWidth(),
                bitmap.getHeight(),男,真);
        返回位图;
    }否则如果(方向== ExifInterface.ORIENTATION_ROTATE_90){
        m.postRotate(90);
        位图= Bitmap.createBitmap(位图,0,0,bitmap.getWidth(),
                bitmap.getHeight(),男,真);
        返回位图;
    }否则如果(方向== ExifInterface.ORIENTATION_ROTATE_270){
        m.postRotate(270);
        位图= Bitmap.createBitmap(位图,0,0,bitmap.getWidth(),
                bitmap.getHeight(),男,真);
        返回位图;
    }
    返回位图;
}


解决方案

我实现了,你可以拍摄照片并设置照片的方向一张照片取的活动。它是由我每次测试,包括三星Galaxy系列,平板电脑,索尼Xperia系列,平板电脑设备支持。

您可以检查出我接受的答案关于这个话题的图像旋转:

<一个href=\"http://stackoverflow.com/questions/18203369/camera-capture-orientation-on-samsung-devices-in-android/18203715#18203715\">Camera在Android的三星捕捉设备定向

如果您还需要保存和使用已旋转的形象,保存和使用附加照片功能,我的回答我上面给:

savePhoto功能:

 公共无效savePhoto(BMP位图){        imageFileFolder =新的文件(Environment.getExternalStorageDirectory()
                cc.getDirectoryName());
        imageFileFolder.mkdir();
        FileOutputStream中走出= NULL;
        日历C = Calendar.getInstance();
        字符串日期= fromInt(c.get(的Calendar.MONTH))
                + fromInt(c.get(Calendar.DAY_OF_MONTH))
                + fromInt(c.get(Calendar.YEAR))
                + fromInt(c.get(Calendar.HOUR_OF_DAY))
                + fromInt(c.get(Calendar.MINUTE))
                + fromInt(c.get(Calendar.SECOND));
        映像文件名称=新的文件(imageFileFolder,与Date.toString()+.JPG);
        尝试{
            OUT =新的FileOutputStream(映像文件名称);
            bmp.com preSS(Bitmap.Com pressFormat.JPEG,70出);
            了out.flush();
            out.close();
            scanPhoto(imageFileName.toString());
            出= NULL;
        }赶上(例外五){
            e.printStackTrace();
        }
    }

scanPhoto功能:

 公共无效scanPhoto(最后弦乐映像文件名称){
        geniusPath =映像文件名称;
        msConn =新MediaScannerConnection(MyClass.this,
                新MediaScannerConnectionClient(){
                    公共无效onMediaScannerConnected(){
                        msConn.scanFile(映像文件名称,NULL);                    }                    @覆盖
                    公共无效onScanCompleted(字符串路径,开放的URI){                        msConn.disconnect();                    }
                });
        msConn.connect();
    }

SavePhotoTask类:

 类SavePhotoTask扩展的AsyncTask&LT;字节[],字符串,字符串&GT; {
        @覆盖
        保护字符串doInBackground(字节[] ... JPEG){
            文件照片=新的文件(Environment.getExternalStorageDirectory()
                    photo.jpg);
            如果(photo.exists()){
                photo.delete();
            }
            尝试{
                FOS的FileOutputStream =新的FileOutputStream(photo.getPath());
                fos.write(JPEG [0]);
                fos.close();
            }赶上(java.io.IOException异常五){
            }
            回报(NULL);
        }
    }

Hello I am working on one android app where I need to capture the image using camera intent and set the bitmap in the imageview but here bitmap is rotated by 90 degree. I have checked many threads of stackoverflow like Photo rotate 90 degree while capture in some phones but did not work for me.

Here when I am executing this exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); then it is returning 0 ORIENTATION_UNDEFINED and in my getImage function no condition is satisfying.

Intent cameraIntent = new Intent(
        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
capturedPhotoName = System.currentTimeMillis() + ".png";
File photo = new File(Environment.getExternalStorageDirectory(),
        capturedPhotoName);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(cameraIntent, CAMERA_INTENT_REQUEST);

onActivityResult

Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
ContentResolver cr = getContentResolver();
Bitmap bitmap;
try {
    bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr,
            selectedImage);
    bitmap = Util.getImage(bitmap, selectedImage.toString());
    mPictureImageView.setImageBitmap(bitmap);
} catch (Exception e) {
    Log.e("New Issue Activity", e.toString());
}

/**
 * Get the image orientation
 * 
 * @param imagePath
 * @return orietation angle
 * @throws IOException 
 */
public static Bitmap getImage(Bitmap bitmap, String path) throws IOException {
    Matrix m = new Matrix();
    ExifInterface exif = new ExifInterface(path);
    int orientation = exif
            .getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
    if ((orientation == ExifInterface.ORIENTATION_ROTATE_180)) {
        m.postRotate(180);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                bitmap.getHeight(), m, true);
        return bitmap;
    } else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
        m.postRotate(90);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                bitmap.getHeight(), m, true);
        return bitmap;
    } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
        m.postRotate(270);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                bitmap.getHeight(), m, true);
        return bitmap;
    }
    return bitmap;
}

解决方案

I implemented one photo take activity which you can take the photo and set the orientation of the photo. It is supported by every device I tested including Samsung galaxy series, tablets, sony xperia series, tablets.

You can check out my accepted answer about rotation of images on this topic:

Camera capture orientation on samsung devices in android

If you also need to save and use that image that you have rotated, saving and using the photo functions additional to my answer I gave above:

savePhoto function:

public void savePhoto(Bitmap bmp) {

        imageFileFolder = new File(Environment.getExternalStorageDirectory(),
                cc.getDirectoryName());
        imageFileFolder.mkdir();
        FileOutputStream out = null;
        Calendar c = Calendar.getInstance();
        String date = fromInt(c.get(Calendar.MONTH))
                + fromInt(c.get(Calendar.DAY_OF_MONTH))
                + fromInt(c.get(Calendar.YEAR))
                + fromInt(c.get(Calendar.HOUR_OF_DAY))
                + fromInt(c.get(Calendar.MINUTE))
                + fromInt(c.get(Calendar.SECOND));
        imageFileName = new File(imageFileFolder, date.toString() + ".jpg");
        try {
            out = new FileOutputStream(imageFileName);
            bmp.compress(Bitmap.CompressFormat.JPEG, 70, out);
            out.flush();
            out.close();
            scanPhoto(imageFileName.toString());
            out = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

scanPhoto function:

public void scanPhoto(final String imageFileName) {
        geniusPath = imageFileName;
        msConn = new MediaScannerConnection(MyClass.this,
                new MediaScannerConnectionClient() {
                    public void onMediaScannerConnected() {
                        msConn.scanFile(imageFileName, null);

                    }

                    @Override
                    public void onScanCompleted(String path, Uri uri) {

                        msConn.disconnect();

                    }
                });
        msConn.connect();
    }

SavePhotoTask class:

class SavePhotoTask extends AsyncTask<byte[], String, String> {
        @Override
        protected String doInBackground(byte[]... jpeg) {
            File photo = new File(Environment.getExternalStorageDirectory(),
                    "photo.jpg");
            if (photo.exists()) {
                photo.delete();
            }
            try {
                FileOutputStream fos = new FileOutputStream(photo.getPath());
                fos.write(jpeg[0]);
                fos.close();
            } catch (java.io.IOException e) {
            }
            return (null);
        }
    }

这篇关于图片使用相机的意图旋转90度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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