呃
目前創新互聯已為上千的企業提供了網站建設、域名、雅安服務器托管、網站托管、服務器租用、企業網站設計、鳳凰網站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協力一起成長,共同發展。
現在想到的辦法是
不用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();
處理異常就不寫了,直接跑了,你應該會吧
本文標題:java圖片變成黑白代碼,java圖片變成黑白代碼了
URL標題:http://m.kartarina.com/article20/hegejo.html
成都網站建設公司_創新互聯,為您提供企業網站制作、手機網站建設、品牌網站制作、云服務器、關鍵詞優化、搜索引擎優化
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯