图片/视频获取缩略图的几种方式

1. 通过Exif获取图片缩略图:

通过path,我们从exif信息中获取缩略图:

    public static Bitmap fromExif(final String filePath, final int width, final int height) {
        if (filePath == null) {
            return null;
        }

        ExifInterface exifIF;
        try {
            exifIF = new ExifInterface(filePath);
        } catch (IOException e) {
            exifIF = null;
        }

        byte[] source = null;
        Matrix mat = null;
        if (exifIF != null) {
            source = exifIF.getThumbnail();
            mat = getMatrixToRotate(exifIF);
        }

        if (source == null) {
            return null;
        }
        Bitmap bitmap = BitmapFactory.decodeByteArray(source, 0, source.length);
        if (bitmap == null) {
            return null;
        }
        Bitmap ret = resizeForThumbnail(bitmap, width, height, mat);
        return ret;
    }

getMatrixToRotate是从exif信息中获取旋转角度并生成矩阵用来,因为我们从exif中拿到的缩略图可能旋转角度不对,因此需要从exif中获取大图的旋转角度,然后可以对缩略图旋转,getMatrixToRotate代码如下:

    public static Matrix getMatrixToRotate(ExifInterface exifIf) {
        if (exifIf == null) {
            return null;
        }
        Matrix ret = new Matrix();
        int orientation = exifIf.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                ret.postRotate(90);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                ret.postRotate(180);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                ret.postRotate(270);
                break;
            default:
                ret = null;
                break;
        }
        return ret;
    }

拿到缩略图的byte数组后转化为bitmap,之后对bitmap进行缩放和旋转,这样就得到了我们最终想要的缩略图:

    public static Bitmap resizeForThumbnail(Bitmap source, final int width, final int height,Matrix matrix) {
        int w = source.getWidth();
        int h = source.getHeight();
        if (w > h) {
            source = Bitmap.createBitmap(source, (w - h) / 2, 0, h, h,matrix,false);
        } else if (w < h) {
            source = Bitmap.createBitmap(source, 0, (h - w) / 2, w, w,matrix,false);
        }
        return source;
    }

2. 从ContentUri获取缩略图

首先从content uri获取到图片的真实路径:

    public static String getFilePathFromContentUri(Uri contentUri, ContentResolver contentResolver) {
        String filePath;
        String[] filePathColumn = {MediaStore.MediaColumns.DATA};

        Cursor cursor = contentResolver.query(contentUri, filePathColumn, null, null, null);
//      也可用下面的方法拿到cursor
//      Cursor cursor = this.context.managedQuery(contentUri, filePathColumn, null, null, null);

        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        filePath = cursor.getString(columnIndex);
        cursor.close();
        return filePath;
    }

获取到filepath之后就可以利用标题1,从Exif获取缩略图,

3. 通过大图缩放生成缩略图

并不是所有的图片的exif信息中都包含缩略图,如果我们通过exif获取到的缩略图为空,就只能通过大图缩放获取缩略图。

    public static Bitmap fromFilePath(final Context context, final String filePath, final int width,
                                 final int height) {
        Bitmap bitmap = decodeScaled(filePath, width, height);
        int degree = getExifOrientation(filePath);
        Matrix matrix = new Matrix();
        matrix.postRotate(degree);
        Bitmap ret = resizeForThumbnail(bitmap, width, height, matrix);
        if (bitmap != ret) {
            bitmap.recycle();
        }
        return ret;
    }

还是不能忘记旋转缩略图:

    public static Bitmap decodeScaled(String filePath, int width, int height) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inJustDecodeBounds = true;

        BitmapFactory.decodeFile(filePath, opt);
        int scaleW = opt.outWidth / width;
        int scaleH = opt.outHeight / height;
        opt.inSampleSize = Math.min(scaleW, scaleH);
        opt.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath, opt);
    }

4. 获取视频缩略图:

    public static Bitmap videoThumbnailFromPath(String path, int width, int height) {
        Bitmap bmp = null;
        try {
            MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
            mediaMetadataRetriever.setDataSource(path);
            bmp = mediaMetadataRetriever.getScaledFrameAtTime(0, MediaMetadataRetriever.OPTION_CLOSEST_SYNC, 512, 384);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (bmp == null) {
            return null;
        }
        Bitmap ret = resizeForThumbnail(bmp, width, height,null);
        bmp.recycle();
        return ret;
    }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值