public static void conver(String filePath) throws IOException {
int xs = 0;
int ys = 0;
if(Test1.xstar != -1){
xs = Test1.xstar;
ys = Test1.ystar;
}
try {
File te = new File("2.jpg");
te.delete();
}catch (Exception e){
}
File file=new File(filePath);
int[] rgb = new int[3];
BufferedImage bi = null;
try {
bi = ImageIO.read(file);
} catch (Exception e) {
e.printStackTrace();
}
int width = bi.getWidth();
int height = bi.getHeight();
int minx = bi.getMinX();
int miny = bi.getMinY();
System.out.println("正在处理:"+file.getName());
//获取初始位置像素点颜色值
int[] rgbstar = new int[3];
int pixel_1 = bi.getRGB(xs, ys);
rgbstar[0] = (pixel_1 & 0xff0000) >> 16;
rgbstar[1] = (pixel_1 & 0xff00) >> 8;
rgbstar[2] = (pixel_1 & 0xff);
for (int i = minx; i < width; i++) {
for (int j = miny; j < height; j++) {
int pixel = bi.getRGB(i, j);
rgb[0] = (pixel & 0xff0000) >> 16;
rgb[1] = (pixel & 0xff00) >> 8;
rgb[2] = (pixel & 0xff);
//计算与起始点的色差
int redgap = rgb[0] - rgbstar[0];
int greengap = rgb[1] - rgbstar[1];
int bluegap = rgb[2] - rgbstar[2];
redgap *= redgap;
greengap *= greengap;
bluegap *= bluegap;
double gap=Math.sqrt(redgap+greengap+bluegap);
//不满足设定alpha为0
if(gap > Test1.gaplim){
bi.setRGB(i, j,new Color(255,0,0,0).getRGB());
}
}
}
System.out.println("\t处理完毕:"+file.getName());
System.out.println();
FileOutputStream ops = new FileOutputStream(new File("2.jpg"));
ImageIO.write(bi,"jpg", ops);
ops.flush();
ops.close();
}
调用这个函数,代码中红色部分setRGB处设置的alpha值,但是不论这里alpha设置什么值,对图片的效果都没有任何影响,小白求解。。