呃
目前创新互联已为上千的企业提供了网站建设、域名、雅安服务器托管、网站托管、服务器租用、企业网站设计、凤凰网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
现在想到的办法是
不用setEnabled
自己把他的监听 给改了 达到和setEnabled的效果 点击没有用 但是图片还是彩色的
试一下吧
代码如下:
package com.baidu.demo019;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class App extends JFrame {
private static final long serialVersionUID = 1L;
public App() {
this.setSize(500, 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Box box = Box.createVerticalBox();
this.add(box);
// 源图像路径
String imageFile = "images/demo019.bmp";
// 源图像
BufferedImage image1 = getImage(imageFile);
JLabel label1 = new JLabel(new ImageIcon(image1));
JPanel panel1 = new JPanel(new BorderLayout());
panel1.add(label1);
box.add(panel1);
// 转换后的图像
Image image2 = translateImage(image1);
JLabel label2 = new JLabel(new ImageIcon(image2));
JPanel panel2 = new JPanel(new BorderLayout());
panel2.add(label2);
box.add(panel2);
}
BufferedImage getImage(String imageFile) {
BufferedImage image = null;
try {
image = ImageIO.read(new File(imageFile));
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
// 转换图像 黑底白字转换为白底黑字,白色设置为透明色
private Image translateImage(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
BufferedImage target = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
for (int i = 0; i width; i++) {
for (int j = 0; j height; j++) {
int val = image.getRGB(i, j);
int red = (val 16) 0xff;
int green = (val 8) 0xff;
int blue = val 0xff;
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
int alpha = 0xff;
if ((red + green + blue) / 3 = 0xff) {
alpha = 0x00;
}
int pixel = (alpha 24) | (red 16) | (green 8) | (blue);
target.setRGB(i, j, pixel);
}
}
return target;
}
public static void main(String[] args) {
new App().setVisible(true);
}
}
运行结果:
//功能已实现
//没什么好注释的,都是API,请君自行查看
import java.awt.Image;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImageUtil {
public static void main(String[] args) {
File file=new File("C:\\Documents and Settings\\Administrator\\桌面\\cd.jpg");
changeImge(file);
}
/**
* * 转换图片 * *
*/
public static void changeImge(File img) {
try {
Image image = ImageIO.read(img);
int srcH = image.getHeight(null);
int srcW = image.getWidth(null);
BufferedImage bufferedImage = new BufferedImage(srcW, srcH,BufferedImage.TYPE_3BYTE_BGR);
bufferedImage.getGraphics().drawImage(image, 0,0, srcW, srcH, null);
bufferedImage=new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY),null).filter(bufferedImage,null);
FileOutputStream fos = new FileOutputStream(img);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
encoder.encode(bufferedImage);
fos.close();
// System.out.println("转换成功...");
} catch (IOException e) {
e.printStackTrace();
throw new IllegalStateException("图片转换出错!", e);
}
}
}
帮你搜了一段网上流行的代码: 灰度变换 下面的程序使用三种方法对一个彩色图像进行灰度变换,变换的效果都不一样。一般而言,灰度变换的算法是将象素的三个颜色分量使用R*0.3+G*0.59+B*0.11得到灰度值,然后将之赋值给红绿蓝,这样颜色取得的效果就是灰度的。另一种就是取红绿蓝三色中的最大值作为灰度值。java核心包也有一种算法,但是没有看源代码,不知道具体算法是什么样的,效果和上述不同。 /* GrayFilter.java*/ /*@author:cherami */ /*email:cherami@163.net*/ import java.awt.image.*; public class GrayFilter extends RGBImageFilter { int modelStyle; public GrayFilter() { modelStyle=GrayModel.CS_MAX; canFilterIndexColorModel=true; } public GrayFilter(int style) { modelStyle=style; canFilterIndexColorModel=true; } public void setColorModel(ColorModel cm) { if (modelStyle==GrayModel else if (modelStyle==GrayModel } public int filterRGB(int x,int y,int pixel) { return pixel; } } /* GrayModel.java*/ /*@author:cherami */ /*email:cherami@163.net*/ import java.awt.image.*; public class GrayModel extends ColorModel { public static final int CS_MAX=0; public static final int CS_FLOAT=1; ColorModel sourceModel; int modelStyle; public GrayModel(ColorModel sourceModel) { super(sourceModel.getPixelSize()); this.sourceModel=sourceModel; modelStyle=0; } public GrayModel(ColorModel sourceModel,int style) { super(sourceModel.getPixelSize()); this.sourceModel=sourceModel; modelStyle=style; } public void setGrayStyle(int style) { modelStyle=style; } protected int getGrayLevel(int pixel) { if (modelStyle==CS_MAX) { return Math.max(sourceModel.getRed(pixel),Math.max(sourceModel.getGreen(pixel),sourceModel.getBlue(pixel))); } else if (modelStyle==CS_FLOAT){ return (int)(sourceModel.getRed(pixel)*0.3+sourceModel.getGreen(pixel)*0.59+sourceModel.getBlue(pixel)*0.11); } else { return 0; } } public int getAlpha(int pixel) { return sourceModel.getAlpha(pixel); } public int getRed(int pixel) { return getGrayLevel(pixel); } public
throws IOException
InputStream in=new InputStream(文件);
OutputStream out=new OutputStream();
StringBuilder sb=new StringBuilder();
while((ch=in.read)!=-1)
{
if(sb.length()!=8)
{
sb.append(ch);
}
else
{
String str=sb.toString();
if(Integer.toHexString(Integer.parseInt(str)).equals("FFFFFF"))
System.out.println("#");
else if(Integer.toHexString(Integer.parseInt(str)).equals("000000"))
System.out.println(" ");
sb=sb.del(0,sb.length());
}
}
in.close();
out.close();
处理异常就不写了,直接跑了,你应该会吧