将白色背景图片变透明
--非常牛:
package test;import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.regex.Pattern;import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;public class Picture {public static void convert(String path) {// TODO Auto-generated constructor stubtry {BufferedImage image = ImageIO.read(new File(path));ImageIcon imageIcon = new ImageIcon(image);BufferedImage bufferedImage = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(),BufferedImage.TYPE_4BYTE_ABGR);Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();g2D.drawImage(imageIcon.getImage(), 0, 0,imageIcon.getImageObserver());int alpha = 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);if (colorInRange(rgb))alpha = 0;elsealpha = 255;rgb = (alpha << 24) | (rgb & 0x00ffffff);bufferedImage.setRGB(j2, j1, rgb);}}g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());// 生成图片为PNGString outFile = path.substring(0, path.lastIndexOf("."));ImageIO.write(bufferedImage, "png", new File(outFile + ".png"));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static boolean colorInRange(int color) {int red = (color & 0xff0000) >> 16;int green = (color & 0x00ff00) >> 8;int blue = (color & 0x0000ff);if (red >= color_range && green >= color_range && blue >= color_range)return true;return false;}public static int color_range = 210;public static Pattern pattern = Pattern.compile("[0-9]*");public static boolean isNo(String str) {return pattern.matcher(str).matches();}/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubString path = JOptionPane.showInputDialog(null, "请输入图片目录");if (path == null || !new File(path).isDirectory()) {JOptionPane.showMessageDialog(null, "输入目录有误!");return;}String color = JOptionPane.showInputDialog(null, "请输入色差范围0~255(建议10~50)");if (isNo(color)) {color_range = 255 - Integer.parseInt(color);File file = new File(path);String[] files = file.list();for (int i = 0; i < files.length; i++) {String ext = files[i].substring(files[i].lastIndexOf(".") + 1);if (ext.equals("jpg")) {convert(path + "//" + files[i]);}}JOptionPane.showMessageDialog(null, "转换完成!");} else {JOptionPane.showMessageDialog(null, "输入的数字有误!");}}}
将白色背景图片变透明相关推荐
- 图片怎么去底色变透明?怎么把图片变透明背景?
我们在使用图片素材的时候,都是需要把图片背景变透明,而且往往处理图片的时候都需要批量图片去底色,那么有没有简单的方法呢?下面就介绍一款图片去底色(https://www.yasuotu.com/but ...
- 怎样将图片的背景色换成透明的?怎么让白底图片变透明?
想要将图片的背景色换成透明的,该怎么处理呢?其实除了使用ps之外,还有一种比较简单的图片去白底变透明的方法,今天就给大家详细讲解一下图片转化透明底的步骤,使用专业的图片去底色(https://www ...
- 如何去除图片中的白色背景(变透明)
很多时候,写小程序会用到一些图片素材,你可能会遇到这样的问题--这些图片,放上去之后,跟我们预期想象的不太一样,图片有白色的方框背景,让自己的整个界面变得很难看.这样的问题对于那些会修图的大神,简直就 ...
- 怎么把背景图片变透明html,怎样把图片背景变成透明
大家好,我是时间财富网智能客服时间君,上述问题将由我为大家进行解答. 以Photoshop软件为例,把图片背景变成透明的方法是: 1.首先,打开电脑中的Photoshop软件,进入主界面: 2.在Ph ...
- 背景图片hover加蒙层_用hover怎么将li的背景图片变透明,文字不变,是背景图片,不是img插入...
匿名用户 1级 2017-01-19 回答 1. h3,ul,li{ margin:0;无外补丁 padding:0;无内补丁} 2. ul,li{ list-style:none; 无列表样式} 3 ...
- PS把图片变透明做logo
Adobe Photoshop 2020 左上角点击选择 选择 色彩范围 颜色容差改为200 点击一下图片 然后 ctrl+shift+I 反选 后 按 delete 导出为png图片 wps打开图片 ...
- 如何把照片背景变成透明?怎么把图片去白底变透明?
平时当我们为文档.表格或幻灯片添加图片时,要是想将图片背景变透明该怎么处理呢?其实可以使用图片去底色(https://www.yasuotu.com/buttonColor)工具来操作,今天推荐这款图 ...
- PhotoShop 选中区域 变透明 透明化 导出图片
文章目录 Intro 相关软件 图片的透明部分 PhotoShop 选中图片部分区域 将其变透明 导出图片 打开图片 对图层"去锁" 使用索套工具选中指定区域 按Delete删除选 ...
- 图片怎么去底色变透明?在线图片去底色的方法
大多数人都喜欢用拍照记录日常生活,那就少不了要修改图片,比如图片去底色(https://www.yasuotu.com/buttonColor)就是经常用到的功能之一,那么如何快速将图片底色变透明呢 ...
- 图片如何去掉背景色?如何使图片背景变透明?
图片设计工作者常常需要用透明背景图片,如果下载的图片素材不是透明背景的话,就需要先用图片处理工具将图片背景变透明.下面我们就使用压缩图的图片去底色(https://www.yasuotu.com/bu ...
最新文章
- hdoj 5199 Gunner map
- Android Layer List 使用实现实例
- 0122 - EOS 编程学习日志(1)
- 嘉实多RO150合成齿轮油
- oracle 批量杀死 死锁进程
- T1013 求先序排列 codevs
- 基于OneAPM的Web系统性能监测
- 车险赔偿需要被保险人签字吗?
- VB6 GDI+ 入门教程[7] Graphics 其他内容
- paip.svn服务服务器安装配置
- spss因子分析结果解读_【SPSS数据分析】SPSS聚类分析(R型聚类)的软件操作与结果解读 ——【杏花开生物医药统计】...
- LAMP架构超详细搭建步骤
- matlab 偏相关系数,偏相关分析(spss偏相关性分析结果解读)
- 基于Raspberry pi的BMI160驱动程序实例
- 钉钉小程序踩坑:(与微信小程序的区别)
- 在三角形中rt是什么意思_【rt三角形什么意思】作文写作问答 - 归教作文网
- unity-shader之混合模式、面剔除、Alpha测试、深度测试、通道遮罩
- Xcode8 及 iOS 10 的适配
- Inflated 3D ConvNet 【I3D】
- 连线9宫格可实现手势密码等