❶ 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();
处理异常就不写了,直接跑了,你应该会吧