三星手机拍照旋转问题 调用相机拍照出现横竖屏切换?

在三星手机上,调用相机拍照可能出现横竖屏切换导致图片显示异常的问题。通过读取图片旋转角度并进行反向旋转,可以修复这一问题。文章介绍了具体的解决方案,包括拍照后图片的处理步骤和相关Markdown编辑器的特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

三星手机拍照旋转问题 调用相机 拍照出现横竖屏切换?


拍照获取图片时,当应用是竖屏时,在部分手机上,如:三星note3上,图片不能正常显示,会旋转90°。

=_=

大概的思路就是拍照后获取图片旋转的角度,然后再回转同样的角度,一般均为0

1.调用拍照后,读取临时存储的图片,temp为自定义的路径
调用拍照

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//下面这句指定调用相机拍照后的照片存储的路径
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri
            .fromFile(new File(Environment
 .getExternalStorageDirectory(),
SQConstants.tempImgFile)));// SQConstants.tempImgFile文件名public static final String tempImgFile = "/avatar.png";
//intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
//intent.putExtra("return-data", true);
startActivityForResult(intent, SQConstants.AVATAR_CAMERA);//SQConstants.AVATAR_CAMERA为常量值,在返回activity时使用,可自行定义

读取图片Bitmap bm = BitmapsUtil.decodeFile(temp, 100);

 private static final int DEFAULT_REQUIRED_SIZE = 70;
 public static Bitmap decodeFile(File f, int size) {
  try {
   BitmapFactory.Options option = new BitmapFactory.Options();
   /**
    inJustDecodeBounds如果将其设为true的话,在decode时将会返回null。
    通过此设置可以去查询一个bitmap的属性,比如bitmap的长和宽,而不占用内存大小.同时可避免OOM
   */
   option.inJustDecodeBounds = true;
   FileInputStream stream1 = new FileInputStream(f);
   BitmapFactory.decodeStream(stream1, null, option);
   stream1.close();
   final int REQUIRED_SIZE = size > 0 ? size : DEFAULT_REQUIRED_SIZE;
   int width_tmp = option.outWidth, height_tmp = option.outHeight;
   int scale = 1;
   while (true) {
    if (width_tmp / 2 < REQUIRED_SIZE
      || height_tmp / 2 < REQUIRED_SIZE)
     break;
    width_tmp /= 2;
    height_tmp /= 2;
    scale *= 2;
   }
   if (scale >= 2) {
    scale /= 2;
   }
   BitmapFactory.Options option2 = new BitmapFactory.Options();
   option2.inSampleSize = scale;
   FileInputStream stream2 = new FileInputStream(f);
   Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, option2);
   stream2.close();
   return bitmap;
  } catch (FileNotFoundException e) {
  } catch (IOException e) {
   e.printStackTrace();
  }
  return null;
 }

2.获取图片旋转的角度,然后给它旋转回来
int degree = BitmapsUtil.readPictureDegree(temp.getAbsolutePath());

/**
  * 获取图片信息
  * 
  * @param path
  * @return
  */
 public static int readPictureDegree(String path) {
  int degree = 0;
  try {
   ExifInterface exifInterface = new ExifInterface(path);
   int orientation = exifInterface.getAttributeInt(
     ExifInterface.TAG_ORIENTATION,
     ExifInterface.ORIENTATION_NORMAL);
   switch (orientation) {
   case ExifInterface.ORIENTATION_ROTATE_90:
    degree = 90;
    break;
   case ExifInterface.ORIENTATION_ROTATE_180:
    degree = 180;
    break;
   case ExifInterface.ORIENTATION_ROTATE_270:
    degree = 270;
    break;
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
  return degree;

 }

3.根据指定旋转度数进行图片旋转
Bitmap bitmap = BitmapsUtil.rotaingImageView(degree, bm);

 /**
  * 图片旋转
  * 
  * @param angle
  * @param bitmap
  * @return
  */
 public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
  // 旋转图片 动作
  Matrix matrix = new Matrix();
  matrix.postRotate(angle);
  System.out.println("angle=" + angle);
  // 创建新的图片
  Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
    bitmap.getWidth(), bitmap.getHeight(), matrix, true);
  return resizedBitmap;
 }

4.存储旋转后图片
compressHeadPhoto(bitmap);

 private File rotateFile;
 private void compressHeadPhoto(final Bitmap bm) {
  rotateFile = new File(Environment.getExternalStorageDirectory(),
                "rotate.png");
  try {
   bm.compress(Bitmap.CompressFormat.PNG, 70, new FileOutputStream(
     rotateFile));
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
 }

5.调用图片裁切
startPhotoZoom(Uri.fromFile(rotateFile));

   /**
     * 裁剪图片方法实现
     * @param uri
     */
    public void s
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值