一个bitmap工具类

public class BitmapUtil {

    private static volatile BitmapUtil instance;

    private BitmapUtil() {
    }

    public static BitmapUtil getInstance() {
        if (instance == null) {
            synchronized (BitmapUtil.class) {
                if (instance == null) {
                    instance = new BitmapUtil();
                }
            }
        }
        return instance;
    }

    /**
     * decodeFile4.4以上直接使用的FileInputStream,需要多次读取
     * decodeStream可以使用BufferedInputStream提高读写性能
     *
     * @param is io流
     * @param isRGB  是否使用RGB_565压缩图片
     * @return bitmap
     */
    public Bitmap getBitmap(InputStream is, boolean isRGB) {
        if (isRGB) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            return BitmapFactory.decodeStream(new BufferedInputStream(is),null,options);
        }
        return BitmapFactory.decodeStream(new BufferedInputStream(is));
    }

    /**
     * bitmap缩略图
     * @param is io流
     * @param maxWidth 最大宽度 建议为屏幕宽度
     * @param maxHeight 最大高度 建议为屏幕宽度
     * @param isRGB 是否使用RGB_565压缩图片
     * @return bitmap
     */
    public Bitmap getBitmap(InputStream is,int maxWidth, int maxHeight, boolean isRGB) {
        byte[] bytes = input2Byte(is);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
        options.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);
        if (isRGB) {
            options.inPreferredConfig = Bitmap.Config.RGB_565;
        }
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
    }

    /**
     * 读取资源文件夹下图片
     *
     * @param res getResources
     * @param id  文件id
     * @param isRGB  是否使用RGB_565压缩图片
     * @return bitmap
     */
    public Bitmap getBitmapResources(Resources res, int id, boolean isRGB) {
        TypedValue value = new TypedValue();
        InputStream is = res.openRawResource(id, value);
        return getBitmap(is,isRGB);
    }

    /**
     * 读取资源文件夹下图片的缩略图
     *
     * @param res       getResources
     * @param id        文件id
     * @param maxWidth  最大宽度 建议为屏幕宽度
     * @param maxHeight 最大高度 建议为屏幕宽度
     * @param isRGB     是否使用RGB_565压缩图片
     * @return bitmap
     */
    public Bitmap getBitmapResources(Resources res, int id, int maxWidth, int maxHeight, boolean isRGB) {
        TypedValue value = new TypedValue();
        InputStream is = res.openRawResource(id, value);
        return getBitmap(is,maxWidth,maxHeight,isRGB);
    }

    /**
     * 获取ByteArrayOutputStream字节数组
     *
     * @param is InputStream
     * @return 字节数组
     */
    private byte[] input2Byte(InputStream is) {
        if (is == null) return null;
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int len;
            while ((len = is.read(b, 0, 1024)) != -1) {
                os.write(b, 0, len);
            }
            return os.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 读取sd卡图片
     *
     * @param path     图片文件路径
     * @param fileName 文件名字
     * @return 高清bitmap
     */
    public Bitmap getBitmapFile(String path, String fileName, boolean isRGB) {
        return getBitmapFile(new File(path, fileName),isRGB);
    }

    /**
     * 读取sd卡图片
     *
     * @param file 图片文件
     * @param isRGB 是否使用RGB_565压缩图片
     * @return bitmap
     */
    public Bitmap getBitmapFile(File file, boolean isRGB) {
        try {
            return getBitmap(new FileInputStream(file),isRGB);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 读取sd卡图片的缩略图
     *
     * @param path      图片文件路径
     * @param fileName  文件名字
     * @param maxWidth  最大宽度 建议为屏幕宽度
     * @param maxHeight 最大高度 建议为屏幕宽度
     * @param isRGB     是否使用RGB_565压缩图片
     * @return bitmap
     */
    public Bitmap getBitmapFile(String path, String fileName, int maxWidth, int maxHeight, boolean isRGB) {
        return getBitmapFile(new File(path, fileName), maxWidth, maxHeight, isRGB);
    }

    /**
     * 读取sd卡图片的缩略图
     *
     * @param file      图片文件
     * @param maxWidth  最大宽度 建议为屏幕宽度
     * @param maxHeight 最大高度 建议为屏幕宽度
     * @param isRGB     是否使用RGB_565压缩图片
     * @return bitmap
     */
    public Bitmap getBitmapFile(File file, int maxWidth, int maxHeight, boolean isRGB) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getAbsolutePath(), options);
        options.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);
        if (isRGB) {
            options.inPreferredConfig = Bitmap.Config.RGB_565;
        }
        options.inJustDecodeBounds = false;
        try {
            return BitmapFactory.decodeStream(new BufferedInputStream(new FileInputStream(file)), null, options);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取缩放比例
     *
     * @param options   BitmapFactory.Options
     * @param maxWidth  最大宽度 建议为屏幕宽度
     * @param maxHeight 最大高度 建议为屏幕宽度
     * @return inSampleSize
     */
    private int calculateInSampleSize(BitmapFactory.Options options, int maxWidth, int maxHeight) {
        int height = options.outHeight;
        int width = options.outWidth;
        int inSampleSize = 1;
        while (height > maxHeight || width > maxWidth) {
            height >>= 1;
            width >>= 1;
            inSampleSize <<= 1;
        }
        return inSampleSize;
    }

}

你可能感兴趣的:(安卓基础)