图片上传,部分机型角度不正确问题解决

来源:互联网 发布:windows loader v2.2.2 编辑:程序博客网 时间:2024/05/01 07:29

图片上传,部分机型角度不正确问题解决

前言

现在app大多都有上传图片功能,比如上传图片,发表动态可能还会上传多张图片,那在我做图片上传的时候,出现了部分机型上传图片后,图片的角度是正确的,我的两个测试机小米和华为是没问题的,但是三星的上传后,图片是默认旋转了90°,那这里要讲的就是怎么解决这个问题,在此记录一下,以保证后面再出现类似需求要多考虑一些

解决问题

直接进入正题

当我们上传图片需要做的处理
  • 压缩
  • 判断角度是否正确
  • 如果不角度不对,纠正角度
  • 其他(加水印什么的看具体需求)

解决
以下代码块

`     /** * 压缩图片,处理某些手机拍照角度旋转的问题 * @param context * @param filePath * @param file * @param q * @return * @throws FileNotFoundException */public static File compressImage(Context context, String filePath, File file, int q) throws FileNotFoundException {    Bitmap bm = getSmallBitmap(filePath);    int degree = readPictureDegree(filePath);    LogUtil.e("fengan", "degree==" + degree);    if (degree != 0) {//旋转照片角度        bm = rotateBitmap(bm, degree);    }    FileOutputStream out = new FileOutputStream(file);    bm.compress(Bitmap.CompressFormat.JPEG, q, out);    return file;}`/** * 获取图片角度 * @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;} /** * 旋转照片 * @param bitmap * @param degress * @return */public static Bitmap rotateBitmap(Bitmap bitmap, int degress) {    if (bitmap != null) {        Matrix m = new Matrix();        m.postRotate(degress);        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),                bitmap.getHeight(), m, true);        return bitmap;    }    return bitmap;}    /** * 根据路径获得突破并压缩返回bitmap用于显示 * * @return */public static Bitmap getSmallBitmap(String filePath) {    final BitmapFactory.Options options = new BitmapFactory.Options();    options.inJustDecodeBounds = true;    BitmapFactory.decodeFile(filePath, options);    // Calculate inSampleSize    options.inSampleSize = calculateInSampleSize(options, 480, 800);    // Decode bitmap with inSampleSize set    options.inJustDecodeBounds = false;    return BitmapFactory.decodeFile(filePath, options);}
阅读全文
0 0
原创粉丝点击