Android 在三星手机中拍摄旋转90度的照片

Android 在三星手机中拍摄旋转90度的照片,android,android-camera,android-camera-intent,Android,Android Camera,Android Camera Intent,这张照片旋转90度,同时在三星手机上拍摄,其他手机(HTC)工作正常。请帮我做这个 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, IMAGE_CAPTURE); @Override protected void onActivityResult(int requestCode, int resu

这张照片旋转90度,同时在三星手机上拍摄,其他手机(HTC)工作正常。请帮我做这个

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

如果它真的是一个bug,那么您可能必须手动将其旋转回横向。位图数据总是有一个宽度和一个高度,只需查看数字,如果宽度小于高度,则按照alistair3408的回答旋转图像。

一些设备根据设备方向旋转图像

在这里,我写了一个获得方向和正确比例的图像的常用方法

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

    }
公共位图解码文件(字符串路径){//您可以在此处提供文件路径
智力定向;
试一试{
if(路径==null){
返回null;
}
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
//找到正确的刻度值。它应该是2的幂。
所需的最终int_尺寸=70;
内部宽度=o.向外宽度,高度=o.向外高度;
整数比例=0;
while(true){
如果(宽度\u tmp/2<要求的\u尺寸
||高度(tmp/2<所需尺寸)
打破
宽度_tmp/=2;
高度_tmp/=2;
scale++;
}
//用inSampleSize解码
BitmapFactory.Options o2=新的BitmapFactory.Options();
o2.inSampleSize=刻度;
位图bm=BitmapFactory.decodeFile(路径,o2);
位图=bm;
ExifInterface exif=新的ExifInterface(路径);
方向=exif
.getAttributeInt(ExifInterface.TAG_方向,1);
Log.e(“ExifInteface…………”、“rotation=“+方向”);
//setAttribute(ExifInterface.ORIENTATION\u ROTATE\u 90,90);
Log.e(“方向”,“方向+方向”);
矩阵m=新矩阵();
如果((方向==ExifInterface.orientation\u ROTATE\u 180)){
m、 旋转后(180);
//m.postScale((float)bm.getWidth(),(float)bm.getHeight());
//如果(m.预旋转(90)){
Log.e(“方向内”,“方向+方向”);
bitmap=bitmap.createBitmap(bm,0,0,bm.getWidth(),
bm.getHeight(),m,true);
返回位图;
}else if(方向==ExifInterface.orientation\u ROTATE\u 90){
m、 旋转后(90);
Log.e(“方向内”,“方向+方向”);
bitmap=bitmap.createBitmap(bm,0,0,bm.getWidth(),
bm.getHeight(),m,true);
返回位图;
}
else if(方向==ExifInterface.orientation\u ROTATE\u 270){
m、 旋转后(270);
Log.e(“方向内”,“方向+方向”);
bitmap=bitmap.createBitmap(bm,0,0,bm.getWidth(),
bm.getHeight(),m,true);
返回位图;
} 
返回位图;
}捕获(例外e){
返回null;
}
}
编辑:


此代码没有经过优化,我只是显示了我的一个测试项目中的逻辑代码。

您可以添加到上述解决方案中的另一个内容是
“samsung”.contentEquals(Build.MANUFACTURER)
。如果您知道您的问题仅存在于samsung设备上,您可以合理地确定您需要旋转返回的图像(仅)
if(“samsung”.contentEquals(Build.MANUFACTURER)和&getActivity().getRequestedOrientation()==ActivityInfo.SCREEN\u-ORIENTATION\u-SENSOR\u-picture//&&width>height//)//您知道您需要在这里旋转


你可以“合理地”确信旋转是有保证的。

您希望纵向图像方向吗?不,我希望图像方向与我在纵向模式下拍摄的前一张照片的方向相同,然后应该是纵向和横向。请帮助我在不同的Android设备上相机的纵向方向存在许多不同的错误s、 包括三星。如果可能的话,通过使用旋转的UI元素使用横向和假肖像模式,就像stock camera应用程序一样。@Alex Cohn有没有解决这个问题的方法?我也有这个问题,请看一看,帮我解决了。它工作正常,但会出现“内存不足异常”当我在三星Galaxy tab2(7“)中拍摄连续快照时。请给出一些解决方案。@Hasmukh您可以更改比例大小位图,在这段代码中,我将其设置为Reliableyes,如果我将比例类型设置为8,则其工作正常,但我需要将大图像显示为预览图像,还需要将大图像上传为字节64。@Hasmukh内存错误有时取决于设备堆大小,有时我们必须小心有了这些位图,使用后就需要回收位图。无论我如何拿着我的HTC手机,当我使用此代码时,我都会得到方向=0。有人有同样的问题吗?这不是解决方案。三星的照片总是宽度>高度。错误仅限于portrate模式。
    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;
        }

    }