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. tp根据数据库动态创建微信菜单

    喻可伟的博客   tp根据数据库动态创建微信菜单 $button = array();$class = M('ucenter_menu')->limit(3)->select();//取出 ...

  2. UIAlertView和UIActivityIndicatorView的使用

    UIAlertView用来显示一个对话框,可以设置对话框的标题.文案.按钮的个数和文案,也可以通过实现delegate来监听按钮的的点击操作. 使用UIAlertView时需要注意: self.ale ...

  3. [PHP] - Laravel - Route路由

    前言 这里使用的是Laravel 5 PHP Laravel的路由比较强悍,但也正因如此,不统一而容易凌乱.比如在路由中可以直接写方法操作(破坏封装啊) 以下是个人学习的例子,不供参考 路由中的直接方 ...

  4. FormatFloat 格式化浮点数

    #和0的区别: #是对应位有值显示,无值不显示 0是对应位有值显示,无值显示0 分号后的字符串是对负值的格式化特殊定义:  s := FormatFloat(.);   .);   .);   .); ...

  5. Discrete.Differential.Geometry-An.Applied.Introduction(sig2008)笔记

    -------------------------------------------------------------- Chapter 1: Introduction to Discrete D ...

  6. 真机调试时遇到“Could not launch *** process launch failed: Security”的解决办法

    半年没写ios程序了,打算重新将这块技术捡回来.谁知道写的第一个测试程序在真机上就跑出来因为安全问题不能加载的情况. ios的版本是9.2的.看提示信息是app的启动被ios的安全机制阻挡了. 在手机 ...

  7. Visual Studio 14 初试,vNext

    下了几天的VS 2014 .终于安装上了,花了好几天时间, VS 2014  下载地址, http://www.visualstudio.com/en-us/downloads/visual-stud ...

  8. C#获取真实IP地址实现方法

    通常来说,大家获取用户IP地址常用的方法是: string IpAddress = ""; if((HttpContext.Current.Request.ServerVariab ...

  9. Ubuntu14.04安装搜狗拼音输入法

    删除ibus输入法 sudo apt-get purge ibus sudo apt-get autoremove 安装fcitx和拼音输入法 sudo apt-get install fcitx f ...

  10. Android sdk tool android 命令参数

      命令参数说明: $ ./android --? Error: Flag '--?' is not a valid global flag. Did you mean to specify it a ...