❶ java中改變一副彩色照片的顏色,使之成為黑白照片,如何添加圖片
//功能已實現
//沒什麼好注釋的,都是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);
}
}
}
❷ java如何判斷照片是彩色還是黑白謝謝
給個建議吧,不過我不知道行不行得通,我畢業設計是做圖像處理的
你可以判斷圖片的其中一個像素點,彩色圖一般都是RGB組合成的,格式是那種3*3的矩陣,而黑白圖像的像素點是通過一個固定的公式轉換來的,轉換後的像素點是1*3的矩陣,你可以通過判斷其中一個像素的列的個數來區分彩色還是黑白。。。如果可以的話就給下分吧,,謝謝!
❸ "java識別照片是彩色還是黑白照
給個建議吧,不過我不知道行不行得通,我畢業設計是做圖像處理的 你可以判斷圖片的其中一個像素點,彩色圖一般都是RGB組合成的,格式是那種3*3的矩陣,而黑白圖像的像素點是通過一個固定的公式轉換來的,轉換後的像素點是1*3的矩陣
❹ java識別照片是彩色還是黑白照
你可以判斷圖片的其中一個像素點,彩色圖一般都是RGB組合成的,格式是那種3*3的矩陣,而黑白圖像的像素點是通過一個固定的公式轉換來的,轉換後的像素點是1*3的矩陣
❺ java程序將彩色png圖片轉為黑白的
幫你搜了一段網上流行的代碼: 灰度變換 下面的程序使用三種方法對一個彩色圖像進行灰度變換,變換的效果都不一樣。一般而言,灰度變換的演算法是將象素的三個顏色分量使用R*0.3+G*0.59+B*0.11得到灰度值,然後將之賦值給紅綠藍,這樣顏色取得的效果就是灰度的。另一種就是取紅綠藍三色中的最大值作為灰度值。java核心包也有一種演算法,但是沒有看源代碼,不知道具體演算法是什麼樣的,效果和上述不同。 /* GrayFilter.java*/ /*@author:cherami */ /*email:[email protected]*/ 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:[email protected]*/ 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
❻ java怎麼讀取圖片上所有點的像素,圖片是黑白,我想讀出黑白點然後再輸出#和空格,求代碼
throwsIOException
InputStreamin=newInputStream(文件);
OutputStreamout=newOutputStream();
StringBuildersb=newStringBuilder();
while((ch=in.read)!=-1)
{
if(sb.length()!=8)
{
sb.append(ch);
}
else
{
Stringstr=sb.toString();
if(Integer.toHexString(Integer.parseInt(str)).equals("FFFFFF"))
System.out.println("#");
elseif(Integer.toHexString(Integer.parseInt(str)).equals("000000"))
System.out.println("");
sb=sb.del(0,sb.length());
}
}
in.close();
out.close();
處理異常就不寫了,直接跑了,你應該會吧