呃
目前創(chuàng)新互聯(lián)已為上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管、服務(wù)器租用、企業(yè)網(wǎng)站設(shè)計(jì)、鳳凰網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
現(xiàn)在想到的辦法是
不用setEnabled
自己把他的監(jiān)聽 給改了 達(dá)到和setEnabled的效果 點(diǎn)擊沒有用 但是圖片還是彩色的
試一下吧
代碼如下:
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);
//?轉(zhuǎn)換后的圖像
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;
}
//?轉(zhuǎn)換圖像?黑底白字轉(zhuǎn)換為白底黑字,白色設(shè)置為透明色
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);
}
}
運(yùn)行結(jié)果:
//功能已實(shí)現(xiàn)
//沒什么好注釋的,都是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);
}
/**
* * 轉(zhuǎn)換圖片 * *
*/
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("轉(zhuǎn)換成功...");
} catch (IOException e) {
e.printStackTrace();
throw new IllegalStateException("圖片轉(zhuǎn)換出錯(cuò)!", e);
}
}
}
幫你搜了一段網(wǎng)上流行的代碼: 灰度變換 下面的程序使用三種方法對一個(gè)彩色圖像進(jìn)行灰度變換,變換的效果都不一樣。一般而言,灰度變換的算法是將象素的三個(gè)顏色分量使用R*0.3+G*0.59+B*0.11得到灰度值,然后將之賦值給紅綠藍(lán),這樣顏色取得的效果就是灰度的。另一種就是取紅綠藍(lán)三色中的最大值作為灰度值。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();
處理異常就不寫了,直接跑了,你應(yīng)該會吧