import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File; import javax.imageio.ImageIO;
import javax.swing.ImageIcon; public class ImageUtil { /**
* 图片去白色的背景,并裁切
*
* @param image 图片
* @param range 范围 1-255 越大 容错越高 去掉的背景越多
* @return 图片
* @throws Exception 异常
*/
public static BufferedImage transferAlpha(BufferedImage bufferedImage, int range) throws Exception {
BufferedImage sub = null;
try {
ImageIcon imageIcon = new ImageIcon(bufferedImage);
Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();
g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon
.getImageObserver());
int alpha = 0;
int minX = bufferedImage.getWidth();
int minY = bufferedImage.getHeight();
int maxX = 0;
int maxY = 0; for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage
.getHeight(); j1++) {
for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage
.getWidth(); j2++) {
int rgb = bufferedImage.getRGB(j2, j1); int R = (rgb & 0xff0000) >> 16;
int G = (rgb & 0xff00) >> 8;
int B = (rgb & 0xff);
if (((255 - R) < range) && ((255 - G) < range) && ((255 - B) < range)) { //去除白色背景;
rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff);
} else {
minX = minX <= j2 ? minX : j2;
minY = minY <= j1 ? minY : j1;
maxX = maxX >= j2 ? maxX : j2;
maxY = maxY >= j1 ? maxY : j1;
}
bufferedImage.setRGB(j2, j1, rgb);
}
}
int width = maxX - minX;
int height = maxY - minY;
sub = bufferedImage.getSubimage(minX, minY, width, height);
ImageIO.write(sub, "png", new File("D:/new.png"));
} catch (Exception e) {
e.printStackTrace();
throw e;
} return sub;
}
}

  去掉白色背景,并剪切成透明背景。

二维码生成并抠图

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

public String generateQRCode(String url) {

		checkNotNull(url, ErrorCode.ERROR_ILLEGAL_PARAMTER);

		log.error("二维码生成:url{}", url);
int width = 300;
int height = 300;
String uploadFileToQiniu = null;
Map<EncodeHintType, String> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = null;
try {
bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height, hints);
BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
BufferedImage transferAlpha = ImageUtil.transferAlpha(bufferedImage, 1);
if (transferAlpha == null) {
throw new BusinessException(ErrorCode.ERROR_ALPHA);// 抠图失败
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(transferAlpha, "png", out);
byte[] b = out.toByteArray();
} catch (Exception e) {
throw new BusinessException(ErrorCode.ERROR_QRCODE);
}
return uploadFileToQiniu;
}

  

  

Image 抠图,背景透明处理的更多相关文章

  1. 【原】CSS实现背景透明,文字不透明,兼容所有浏览器

    11.11是公司成立的日子,16岁啦,我呢3岁半,感谢公司给了这样一个平台,让我得以学习和成长,这里祝愿公司发展越来越好~ 进入主题,每年11月11号是光棍节,产生于校园,本来只是一流传于年轻人的娱乐 ...

  2. CSS实现背景透明,文字不透明(兼容各浏览器)

    在 FF/Chrome 等较新的浏览器中可以使用css属性background- color的rgba轻松实现背景透明,而文字保持不透明.而IE6/7/8浏览器不支持rgba,只有使用IE的专属滤镜f ...

  3. CSS实现背景透明,文字不透明,兼容所有浏览器

    11.11是公司成立的日子,16岁啦,我呢3岁半,感谢公司给了这样一个平台,让我得以学习和成长,这里祝愿公司发展越来越好~ 进入主题,每年11月11号是光棍节,产生于校园,本来只是一流传于年轻人的娱乐 ...

  4. css 背景透明文字(内容)不透明三种实现方法

    好久没写博客了.以前还想着最少一个月抽空写几篇.结果没做到O(∩_∩)O~~.好吧.现在努力,继续坚持. 看着以前写的东西,感觉自己在逐渐成长. 先上图: 本文主要记录如上图一样的.文字或内容不透明, ...

  5. VC++ CStatic控件背景透明且改变其文本时,文字重叠解决方法

    最近在项目中将CStatic控件设置为背景透明且在一个定时器函数改变其文本,结果CStatic的文字重叠了.解决该问题的方案是:从CStatic类派生自己的静态文本控件. 其实设置背景透明,也就是在C ...

  6. 用Photoshop处理图片使背景透明

    用Photoshop处理图片使背景透明 打开一张图片 双击背景或者右键背景图层,新建一个图层, 选择魔棒工具,单击图片, 会自动选择颜色相近的范围 按下键盘的delete键,就可以删除魔棒所选择的区域 ...

  7. android 自定义Dialog背景透明及显示位置设置

    先贴一下显示效果图,仅作参考: 代码如下: 1.自定义Dialog public class SelectDialog extends AlertDialog{ public SelectDialog ...

  8. <select>在chrome浏览器下背景透明问题

    在上篇文章<只用CSS美化选择框>运用了背景透明的技巧来美化选择框,但在chrome浏览器下遇到了跟ie.ff不一样的透明效果,下面重现一下: 在一个大的div(背景红色)内放置一个sel ...

  9. canvas背景透明

    var can=document.getElementById("canv"); c=can.getContext("2d"); c.globalAlpha=. ...

  10. CSS实现背景透明,文字不透明(各浏览器兼容)

    /*CSS*/.waps{ background:url(07158.bmp) no-repeat top center fixed; width:1004px; text-align:center; ...

随机推荐

  1. MSI Error 1603 installing AppFabric 1.1 / Win7 x64

    MSI Error 1603 installing AppFabric 1.1 / Win7 x64  Archived Forums A-B > AppFabric Caching   先说解 ...

  2. 免装版tomcat注册成windows系统服务方法

    如果一台服务器要部署两个应用,而且又各自不受影响的话,只能使用两个端口两个tomcat分别管理 在这里吐槽一下tomcat,为毛停止服务就把所有应用都停了,更新其中一个,就要把所有的都停了,然后更新完 ...

  3. 技术|程序员必须要学会Google搜索技巧

    程序员必须要学会Google搜索技巧 摘要: 因为Google在我天朝被墙,学FQ请通过Bing进行搜索如何FQGoogle搜索技巧我曾经多次劝我的另一个朋友花10分钟学习一下Google通配符的使用 ...

  4. C#实现对Windows 服务安装

    Windows服务作用:定时用户消息推送,WEB程序实时统计等 Windows服务创建:C#创建服务的方式也有很多种,建议大家在做之前可以先全面了解一下这方面东西再去动手这样便于解决中间遇到一些比较棘 ...

  5. 智能指针weak_ptr解决循环依赖问题

    #include <iostream> #include <memory> class Woman; class Man{ private: std::weak_ptr< ...

  6. Linux下Tomcat服务器重启与关闭

    Linux下Tomcat重新启动 详细请参照原网站链接http://www.cnblogs.com/tovep/articles/2473147.html 在Linux系统下,重启Tomcat使用命令 ...

  7. Hadoop总结篇之五---模块间是怎么驱动执行的

    在MRv1中,各个模块间驱动运行的方式是函数调用的方式.这是同步的过程,上一模块调用下一模块函数后,等待其执行.效率不高. 在MRv2中做了改进,yarn基于事件驱动的并发模型.在详细介绍前,先看下图 ...

  8. Random

    /* * Random:产生随机数的类 * * 构造方法: * public Random():没有给种子,用的是默认种子,是当前时间的毫秒值 * public Random(long seed):给 ...

  9. eclipse插件

    #eclipse market http://www.eclipse.org/mpc/archive.php http://download.eclipse.org/mpc/mars/ #文件路径 p ...

  10. python(30) 获取网页返回的状态码,状态码对应问题查询

    获取访问网页返回的状态码 html = requests.get(Url) respon = html.status_code 以下内容来自于维基百科:点击查看网页 1xx消息 这一类型的状态码,代表 ...