记录一个好用的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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2024年04月18日 09时20分09秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
【Leetcode优先级队列】-合并K个升序链表
2019-04-26
【多线程与高并发】-Java如何实现一个阻塞队列呢?
2019-04-26
【多线程高并发】-多线程实现数组的读与写
2019-04-26
【Java设计者模式】-Java实现订阅-发布者模式
2019-04-26
【计算机操作系统】-什么是系统调用呢?什么是用户态?什么是内核态?
2019-04-26
【计算机操作系统-进程管理】-进程通信是什么呢?
2019-04-26
Python程序元素分析
2019-04-26
TurtleArt美景图
2019-04-26
margin布局
2019-04-26
盒模型之border实践--三角形
2019-04-26
块状元素与内敛元素
2019-04-26
CSS控制段落和文字属性和背景
2019-04-26
Python语言开发工具
2019-04-26
Requests库的入门
2019-04-26
Robots协议
2019-04-26
Python网络爬虫的网站实例
2019-04-26
HTML学习思维导图
2019-04-26
h标签与p标签
2019-04-26
BeautifulSoup库的安装及基本元素
2019-04-26