记录一个好用的Bitmap工具类
发布日期:2021-07-30 03:26:16 浏览次数:12 分类:技术文章

本文共 4693 字,大约阅读时间需要 15 分钟。

package com.why.project.bitmaputildemo.utils;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Matrix;import android.graphics.PixelFormat;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;public class BitmapUtil {    public static Bitmap temp;    /**根据指定的高度进行缩放(source是bitmap)*/    public static Bitmap bitmapZoomByHeight(Bitmap srcBitmap, float newHeight) {        float scale = newHeight / (((float)srcBitmap.getHeight()));        return BitmapUtil.bitmapZoomByScale(srcBitmap, scale, scale);    }    /**根据指定的高度进行缩放(source是drawable)*/    public static Bitmap bitmapZoomByHeight(Drawable drawable, float newHeight) {        Bitmap bitmap = BitmapUtil.drawableToBitmap(drawable);        float scale = newHeight / (((float)bitmap.getHeight()));        return BitmapUtil.bitmapZoomByScale(bitmap, scale, scale);    }    /**根据指定的宽度比例值和高度比例值进行缩放*/    public static Bitmap bitmapZoomByScale(Bitmap srcBitmap, float scaleWidth, float scaleHeight) {        int width = srcBitmap.getWidth();        int height = srcBitmap.getHeight();        Matrix matrix = new Matrix();        matrix.postScale(scaleWidth, scaleHeight);        Bitmap bitmap = Bitmap.createBitmap(srcBitmap, 0, 0, width, height, matrix, true);        if(bitmap != null) {            return bitmap;        }else {            return srcBitmap;        }    }    /**将drawable对象转成bitmap对象*/    public static Bitmap drawableToBitmap(Drawable drawable) {        int width = drawable.getIntrinsicWidth();        int height = drawable.getIntrinsicHeight();        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;        Bitmap bitmap = Bitmap.createBitmap(width, height, config);        Canvas canvas = new Canvas(bitmap);        drawable.setBounds(0, 0, width, height);        drawable.draw(canvas);        return bitmap;    }    /**将drawable对象转成bitmap对象*/    public static Bitmap drawableToBitmap2(Drawable drawable) {        BitmapDrawable bd = (BitmapDrawable) drawable;        Bitmap bm= bd.getBitmap();        return bm;    }        /**将base64图片对象转成bitmap对象*/        public static Bitmap base64ToBitmap(String string, int optionSize) {        Bitmap bitmap = null;        try {            byte[] bitmapArray = Base64.decode(string.split(",")[1], Base64.DEFAULT);            bitmap = byteToBitmap(bitmapArray, optionSize);        } catch (Exception e) {            e.printStackTrace();        }        return bitmap;    }   public static Bitmap byteToBitmap(byte[] imgByte, int optionSize) {        InputStream input = null;        Bitmap bitmap = null;        BitmapFactory.Options options = new BitmapFactory.Options();        options.inSampleSize = optionSize;        options.inScaled = true;        options.inPreferredConfig = Bitmap.Config.ARGB_8888;        input = new ByteArrayInputStream(imgByte);        SoftReference softReference = new SoftReference(BitmapFactory.decodeStream(input, null, options));        bitmap = (Bitmap) softReference.get();        if (imgByte != null) {            imgByte = null;        }        try {            if (input != null) {                input.close();            }        } catch (IOException e) {            e.printStackTrace();        }        return bitmap;    }    /**将bitmap对象保存成图片到sd卡中*/    public static void saveBitmapToSDCard(Bitmap bitmap, String path) {        File file = new File(path);        if(file.exists()) {            file.delete();        }        try {            FileOutputStream fileOutputStream = new FileOutputStream(file);            bitmap.compress(Bitmap.CompressFormat.PNG, 100, ((OutputStream)fileOutputStream));//设置PNG的话,透明区域不会变成黑色            fileOutputStream.close();            System.out.println("----------save success-------------------");        }        catch(Exception v0) {            v0.printStackTrace();        }    }    /**从sd卡中获取图片的bitmap对象*/    public static Bitmap getBitmapFromSDCard(String path) {        Bitmap bitmap = null;        try {            FileInputStream fileInputStream = new FileInputStream(path);            if(fileInputStream != null) {                BitmapFactory.Options options = new BitmapFactory.Options();                options.inSampleSize = 2; //当图片资源太大的适合,会出现内存溢出。图片宽高都为原来的二分之一,即图片为原来的四分一                bitmap = BitmapFactory.decodeStream(((InputStream) fileInputStream), null, options);            }        } catch(Exception e) {            return null;        }        return bitmap;    }}

转载地址:https://blog.csdn.net/u010886975/article/details/85245606 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Android Studio xml Unknown attribute 错误解决办法,亲测有效
下一篇:Android APP自动更新时,跳转到安装界面时出现解析失败

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月18日 09时20分09秒