拍照与摄像头的意图旋转画面在纵向模式的android [英] Taking picture with camera intent rotate picture in portrait mode android

查看:216
本文介绍了拍照与摄像头的意图旋转画面在纵向模式的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

照片成功拍摄的摄像头,但在三星Galaxy肖像模式S3图像被旋转。我怎样才能解决这个问题。 相机意图是如下:

 意向意图=新的意图(android.media.action.IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(xdestination));
        startActivityForResult(意向,CAMERA_PIC_REQUEST);
 

在对结果的活动。

 如果(要求code == CAMERA_PIC_REQUEST){

            //位图BM =(位图)data.getExtras()获得(数据);

                乌里photo_uri = Uri.fromFile(xdestination);

                Editer.PHOTO_FROM = 11;

                位图德codeSampledBitmapFromFile = NULL;
                尝试 {
                    德codeSampledBitmapFromFile =去codeURI(photo_uri);
                }赶上(FileNotFoundException异常E1){
                    // TODO自动生成的catch块
                    e1.printStackTrace();
                }

                ByteArrayOutputStream字节=新ByteArrayOutputStream();
                德codeSampledBitmapFromFile.com preSS(Bitmap.Com pressFormat.JPEG,100个字节);

                文件F =新的文件(Environment.getExternalStorageDirectory(),user_image.jpg);

                尝试 {

                    如果(f.exists())
                        f.delete();

                    f.createNewFile();

                    FileOutputStream中FO =新的FileOutputStream(F);
                    fo.write(bytes.toByteArray());
                    fo.close();

                }赶上(IOException异常E){

                    e.printStackTrace();
                    Log.d(ERRORe.toString());
                }

            }
 

解决方案

传递图片您拍摄的照片和SD卡路径进入,这将返回正确的导向画面下面的方法...

 私人位图imageOreintationValidator(位图位图,字符串路径){

    ExifInterface EI;
    尝试 {
        EI =新ExifInterface(路径);
        INT方向= ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        开关(方向){
        案例ExifInterface.ORIENTATION_ROTATE_90:
            位图= rotateImage(位图,90);
            打破;
        案例ExifInterface.ORIENTATION_ROTATE_180:
            位= rotateImage(位图,180);
            打破;
        案例ExifInterface.ORIENTATION_ROTATE_270:
            位= rotateImage(位图,270);
            打破;
        }
    }赶上(IOException异常E){
        e.printStackTrace();
    }

    返回的位图;
}

私人位图rotateImage(位图源,浮动角){

    点阵位图= NULL;
    字模=新的Matrix();
    matrix.postRotate(角度);
    尝试 {
        位图= Bitmap.createBitmap(源,0,0,source.getWidth(),source.getHeight(),
                矩阵,真正的);
    }赶上(OutOfMemoryError异常错误){
        err.printStackTrace();
    }
    返回的位图;
}
 

Picture was taken successfully with camera but in portrait mode on samsung galaxy s3 the picture gets rotated. How can i solve this issue. Camera intent is as follows:

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(xdestination));
        startActivityForResult(intent, CAMERA_PIC_REQUEST);

In activity for result

 if (requestCode==CAMERA_PIC_REQUEST){

            //  Bitmap bm = (Bitmap) data.getExtras().get("data"); 

                Uri photo_uri = Uri.fromFile(xdestination);

                Editer.PHOTO_FROM=11;

                Bitmap decodeSampledBitmapFromFile=null;
                try {
                    decodeSampledBitmapFromFile = decodeUri(photo_uri);
                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                decodeSampledBitmapFromFile.compress(Bitmap.CompressFormat.JPEG,100, bytes);

                File f  = new File(Environment.getExternalStorageDirectory(), "user_image.jpg");

                try {

                    if(f.exists())
                        f.delete();

                    f.createNewFile();

                    FileOutputStream fo = new FileOutputStream(f);
                    fo.write(bytes.toByteArray());
                    fo.close();

                } catch (IOException e) {

                    e.printStackTrace( );
                    Log.d("ERROR", e.toString());
                }

            }

解决方案

Pass your taken picture and SDCard path of that picture into the following method which will return the correct oriented picture...

private Bitmap imageOreintationValidator(Bitmap bitmap, String path) {

    ExifInterface ei;
    try {
        ei = new ExifInterface(path);
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            bitmap = rotateImage(bitmap, 90);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            bitmap = rotateImage(bitmap, 180);
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            bitmap = rotateImage(bitmap, 270);
            break;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bitmap;
}

private Bitmap rotateImage(Bitmap source, float angle) {

    Bitmap bitmap = null;
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    try {
        bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
                matrix, true);
    } catch (OutOfMemoryError err) {
        err.printStackTrace();
    }
    return bitmap;
}

这篇关于拍照与摄像头的意图旋转画面在纵向模式的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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