Android图片压缩、加水印
指定图片文件大小压缩
实现
public Bitmap compressAndGenImage(Bitmap image, String outPath, int maxSize)throws IOException {Bitmap bitmap;ByteArrayOutputStream os = new ByteArrayOutputStream();// scaleint options = 100;// Store the bitmap into output stream(no compress)image.compress(Bitmap.CompressFormat.JPEG, options, os);// Compress by loopwhile (os.toByteArray().length / 1024 > maxSize) {// Clean up osos.reset();// 每次质量下降10options -= 10;image.compress(Bitmap.CompressFormat.JPEG, options, os);}ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());bitmap = BitmapFactory.decodeStream(is);// Generate compressed image fileFileOutputStream fos = new FileOutputStream(outPath);fos.write(os.toByteArray());fos.flush();fos.close();is.close();return bitmap;}
优点
这种方式压缩得图片,可以减少文件的存储体积,有利于文件上传。
缺点
失真
首先因为没有改变Bitmap的像素值,所以文件转换成的Bitmap图片压缩后和压缩前生成的Bitmap一样大,这时候在使用的时候就要注意了,一张3000K的图片很可能生成的Bitmap多达10M,使用的时候考虑内存溢出问题。
按照官方的解释:图片重新构造, 但是有可能图像的位深(即色深)和每个像素的透明度会变化,JPEG onlysupports opaque(不透明), 也就是说以jpeg格式压缩后, 原来图片中透明的元素将消失.所以这种格式很可能造成失真。
指定图片宽高大小压缩
作用
一般用来生成缩略图,不会失真
实现
public Bitmap ratio(Bitmap image, float pixelW, float pixelH) {ByteArrayOutputStream os = new ByteArrayOutputStream();image.compress(Bitmap.CompressFormat.JPEG, 100, os);if (os.toByteArray().length / 1024 > 1024) {// 判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出os.reset();// 重置baos即清空baosimage.compress(Bitmap.CompressFormat.JPEG, 50, os);// 这里压缩50%,把压缩后的数据存放到baos中}ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());BitmapFactory.Options newOpts = new BitmapFactory.Options();// 开始读入图片,此时把options.inJustDecodeBounds 设回true了newOpts.inJustDecodeBounds = true;newOpts.inPreferredConfig = Config.RGB_565;Bitmap bitmap = BitmapFactory.decodeStream(is, null, newOpts);newOpts.inJustDecodeBounds = false;int w = newOpts.outWidth;int h = newOpts.outHeight;float hh = pixelH;float ww = pixelW;// 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可int be = 1;// be=1表示不缩放if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放be = (int) (newOpts.outWidth / ww);} else if (w < h && h > hh) {// 如果高度高的话根据宽度固定大小缩放be = (int) (newOpts.outHeight / hh);}if (be <= 0)be = 1;newOpts.inSampleSize = be;// 设置缩放比例// 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了is = new ByteArrayInputStream(os.toByteArray());bitmap = BitmapFactory.decodeStream(is, null, newOpts);return bitmap;}
图标水印
/*** 设置水印图片到中间*/public static Bitmap createWaterMaskCenter(Bitmap src, Bitmap watermark) {return createWaterMaskBitmap(src, watermark,(src.getWidth() - watermark.getWidth()) / 2,(src.getHeight() - watermark.getHeight()) / 2);}public static Bitmap drawTextToLeftTop(Context context, Bitmap bitmap,String text) {return drawTextToLeftTop(context, bitmap, text, 16, Color.RED, 4, 4);}/*** 给图片添加文字到左上角*/public static Bitmap drawTextToLeftTop(Context context, Bitmap bitmap,String text, int size, int color, int paddingLeft, int paddingTop) {Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);paint.setColor(color);paint.setTextSize(dp2px(context, size));Rect bounds = new Rect();paint.getTextBounds(text, 0, text.length(), bounds);return drawTextToBitmap(context, bitmap, text, paint, bounds,dp2px(context, paddingLeft), dp2px(context, paddingTop)+ bounds.height());}
文字水印
/*** 绘制文字到右下角*/public static Bitmap drawTextToRightBottom(Context context, Bitmap bitmap,String text, int size, int color, int paddingRight,int paddingBottom) {Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);paint.setColor(color);paint.setTextSize(dp2px(context, size));Rect bounds = new Rect();paint.getTextBounds(text, 0, text.length(), bounds);return drawTextToBitmap(context,bitmap,text,paint,bounds,bitmap.getWidth() - bounds.width()- dp2px(context, paddingRight), bitmap.getHeight()- dp2px(context, paddingBottom));}
Demo
Android图片压缩、加水印相关推荐
- H5多图片压缩加水印上传
1.环境配置 开发语言:php+H5 框架:thinkPHP3.2+WeUI 服务器:阿里云centos7(客户主机是WD的Windows虚拟主机) 2.场景描述 最近在做微信公众号的全栈开发,涉及到 ...
- java 图片压缩加水印_java图片添加水印和压缩
图片添加水印和压缩 [Java]代码 package net.leebao.core.util; import java.awt.AlphaComposite; import java.awt.Col ...
- java 压缩图片时候加水印
直接上代码: 图片压缩加水印 @Testpublic void TetsShuiYin() {// String path1 = "D:\\temp\\Flower.PNG"; / ...
- Android图片压缩(质量压缩和尺寸压缩)
在网上调查了图片压缩的方法并实装后,大致上可以认为有两类压缩:质量压缩(不改变图片的尺寸)和尺寸压缩(相当于是像素上的压缩):质量压缩一般可用于上传大图前的处理,这样就可以节省一定的流量,毕竟现在的手 ...
- Android图片压缩尺寸和质量
Android在处理图片时,如果不进行压缩处理,很容易就出现OOM内存溢出(OutOfMemory)问题,所以无论是第三方图片加载还是自己在处理图片时,都要进行压缩处理. Android系统中,一张图 ...
- android 图片压缩总结1
在网上调查了图片压缩的方法并实装后,大致上可以认为有两类压缩:质量压缩(不改变图片的尺寸)和尺寸压缩(相当于是像素上的压缩):质量压缩一般可用于上传大图前的处理,这样就可以节省一定的流量,毕竟现在的手 ...
- Android图片压缩库——libjpeg-turbo
为何Android图片压缩效率比IOS低质量差 为什么Android的图片压缩质量要比iPhone的压缩质量差很多,这是因为Android底层犯的一个小错误:libjpeg.并且这个错误一直持续到了今 ...
- 最详细的Android图片压缩攻略
Mr.Louis的博客地址: https://blog.csdn.net/weixin_44005563 最近在研究图片压缩原理,看了大量资料,从上层尺寸压缩.质量压缩原理到下层的哈夫曼压缩,走成华大 ...
- android水印控件,Android图片添加文字水印并保存水印文字图片到指定文件
Android图片添加文字水印并保存水印文字图片到指定文件package zhangphil.test;import android.graphics.Bitmap;import android.gr ...
- android 图片压缩,bitmap压缩总结
1. 图片压缩相关概念 在android开发中,图片加载到内存中通常需要占用大量的内存,导致app性能过度消耗,严重的话可能会导致OOM异常,所以对图片进行优化就尤为重要. android图片压 ...
最新文章
- 初中计算机指导教师意见,初中信息技术教学计划(推荐3篇)
- 浅析Microsoft .net PetShop程序中的购物车和订单处理模块(Profile技术,异步MSMQ消息)转...
- vue github开源项目_11月份Github上最热门的开源项目
- Meanshift解析
- Thinking in java 笔记一
- 数据库设计范式实例解析
- html+css+javascript之间的关系与作用
- 如何用正确的方法来写出质量好的软件的75条体会
- 学习linux之用户-文件-权限操作
- 【转】VS中常用图标提示含义
- python线程等待_python3 中 Event.wait 多线程等待
- 扫地机器人湿地_仅仅是打湿地板而已?定价高了,石头扫地机器人的拖地却仍很一般...
- HDU 2298 Toxophily 【三分算法 or 直接推导物理公式】
- Java I/O系统之Writer
- 【源码】基于MPPT的光伏(PV)系统仿真
- 如果使用CSS创建表格显示
- JS·经典·炫彩菜单(动画效果) for jquery
- 【ACM】心路历程2019.9.29
- re学习笔记(0) CG-CTF-re-3 py交易
- Java SE 基础知识~流程控制