缩略图采集工具类

package com.lt.an20_utils;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.media.MediaMetadataRetriever;
/**
 * Created by 风情万种冷哥哥 on 2016.
 *
 */
public class BitmapThumbnailHelper {

    /**
     * 对图片进行二次采样,生成缩略图。放置加载过大图片出现内存溢出
     */
    public static Bitmap createThumbnail(byte[] data, int newWidth,
            int newHeight) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(data, 0, data.length, options);
        int oldWidth = options.outWidth;
        int oldHeight = options.outHeight;

        // Log.i("Helper", "--->oldWidth:" + oldWidth);
        // Log.i("Helper", "--->oldHeight:" + oldHeight);

        int ratioWidth = 0;
        int ratioHeight = 0;

        if (newWidth != 0 && newHeight == 0) {
            ratioWidth = oldWidth / newWidth;
            options.inSampleSize = ratioWidth;
            // Log.i("Helper", "--->ratioWidth:" + ratioWidth);

        } else if (newWidth == 0 && newHeight != 0) {
            ratioHeight = oldHeight / newHeight;
            options.inSampleSize = ratioHeight;
        } else {
            ratioHeight = oldHeight / newHeight;
            ratioWidth = oldWidth / newWidth;
            options.inSampleSize = ratioHeight > ratioWidth ? ratioHeight
                    : ratioWidth;
        }
        options.inPreferredConfig = Config.ALPHA_8;
        options.inJustDecodeBounds = false;
        Bitmap bm = BitmapFactory
                .decodeByteArray(data, 0, data.length, options);
        return bm;
    }

    public static Bitmap createThumbnail(String pathName, int newWidth,
            int newHeight) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(pathName, options);
        int oldWidth = options.outWidth;
        int oldHeight = options.outHeight;

        int ratioWidth = 0;
        int ratioHeight = 0;

        if (newWidth != 0 && newHeight == 0) {
            ratioWidth = oldWidth / newWidth;
            options.inSampleSize = ratioWidth;
        } else if (newWidth == 0 && newHeight != 0) {
            ratioHeight = oldHeight / newHeight;
            options.inSampleSize = ratioHeight;
        } else {
            ratioHeight = oldHeight / newHeight;
            ratioWidth = oldWidth / newWidth;
            options.inSampleSize = ratioHeight > ratioWidth ? ratioHeight
                    : ratioWidth;
        }
        options.inPreferredConfig = Config.ALPHA_8;
        options.inJustDecodeBounds = false;
        Bitmap bm = BitmapFactory.decodeFile(pathName, options);
        return bm;
    }

    // 获取视频文件的典型帧作为封面
    public static Bitmap createVideoThumbnail(String filePath) {
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setDataSource(filePath);
            bitmap = retriever.getFrameAtTime();
        } catch (Exception ex) {
        } finally {
            try {
                retriever.release();
            } catch (RuntimeException ex) {
            }
        }
        return bitmap;
    }

    // 获取音乐文件中内置的专辑图片
    public static Bitmap createAlbumThumbnail(String filePath) {
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setDataSource(filePath);
            byte[] art = retriever.getEmbeddedPicture();
            bitmap = BitmapFactory.decodeByteArray(art, 0, art.length);
        } catch (Exception ex) {
        } finally {
            try {
                retriever.release();
            } catch (RuntimeException ex) {
            }
        }
        return bitmap;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值