package com.xixi;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DirectColorModel;
import java.awt.image.FilteredImageSource;
import java.awt.image.RGBImageFilter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ChangeBack {
public static void main(String[] args) {
new ChangeBack().changBackPic("D:\\jazm.png");
}
public void changBackPic(String path) {
File file = new File(path);
int height = getPicHeight(file);
int width = getPicWidth(file);
try {
BufferedImage bimg = ImageIO.read(new FileInputStream(path));
FilteredImageSource fis = new FilteredImageSource(bimg.getSource(), new MyFilter(255));
Image im = Toolkit.getDefaultToolkit().createImage(fis);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D ig2 = bi.createGraphics();
ig2.drawImage(im, 0, 0, width, height, null);
ig2.dispose();
String[] array = path.split("\\.");
String gs = array[array.length - 1];
path = path.replace("." + gs, "_changeBack." + gs);
ImageIO.write(bi, "PNG", new File(path));
} catch (Exception e) {
e.printStackTrace();
}
}
public static int getPicHeight(File file) {
try {
return ImageIO.read(new FileInputStream(file)).getHeight();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
public static int getPicWidth(File file) {
try {
return ImageIO.read(new FileInputStream(file)).getWidth();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
}
class MyFilter extends RGBImageFilter {
int alpha = 0;
public MyFilter(int alpha) {
this.canFilterIndexColorModel = true;
this.alpha = alpha;
}
public int filterRGB(int x, int y, int rgb) {
DirectColorModel dcm = (DirectColorModel) ColorModel.getRGBdefault();
int red = dcm.getRed(rgb);
int green = dcm.getGreen(rgb);
int blue = dcm.getBlue(rgb);
int alp = dcm.getAlpha(rgb);
if (red == 255 && blue == 255 && green == 255) {
alpha = 0;
} else {
alpha = 255;
}
return alpha << 24 | red << 16 | green << 8 | blue;
}
}
下载
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于