[英]How to convert Colors to Grayscale in java with just the java.io.*; library?
好吧,我得到了..因为在我的位置,他们这样要求它。.好吧,我有://一些变量和注释是可能的代码。但是我的意思是我不知道该怎么做,我在互联网上发现要转换R是字节在* 0.21中,而G是* 0.71,我猜是蓝色是* 0.07。 我只能使用该库java.io. *; 我正在使用1024 x 768的BMP格式图像,如果您可以帮助我,或者您希望我更具体,请告诉我。 我不知道我是否必须添加其他变量或删除某些变量或进行修改,请帮助我。 我不是那种语言的新手,但我不是专家。 代码就在这里:
导入java.io. *;
公开课灰度{
FileInputStream image;
FileOutputStream img;
byte[] datos;
int i;
int cont;
public Grayscale(String nombre)throws Exception{
this.image = new FileInputStream(nombre);
this.img = img;
this.datos = new byte[image.available()];
this.i = 54;
this.cont = 1;
}
public void gray()throws Exception{
image.read(datos);
img = new FileOutputStream("grayscale.bmp");
while(i<datos.length){
if(cont == 1){
datos[i] = datos[i] ;//* 0.21;
cont++;
} else if(cont == 2){
datos[i] = datos [i] ;//* 0.71;
cont++;
} else if(cont == 3){
datos[i] = datos[i] ;//* 0.07;
cont++;
}else{
cont = 1;
}
i++;
}
img.write(datos);
}
}
大多数图像格式在像素数据之前都有标头信息,因此在读取这些类型的文件时,需要考虑到这一点。
坦白说,在可能的情况下依赖现有的库要容易得多。
ImageIO
允许您读取和写入许多不同的文件格式,包括BMP。
看一眼
下一个决定是-您自己转换图像还是使用预先存在的滤镜。 您必须做一些自己的指标,但是在过去,我发现图像的像素处理速度很慢,至少比内置滤镜要慢...
原始,手动灰度,自动/滤镜灰度
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class GrayScaleImage {
public static void main(String[] args) {
new GrayScaleImage();
}
public GrayScaleImage() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridLayout(0, 3));
try {
BufferedImage master = ImageIO.read(new File("/path/to/file.bmp"));
BufferedImage gray = ImageIO.read(new File("/path/to/file.bmp"));
// Manual manipulation...
for (int x = 0; x < gray.getWidth(); x++) {
for (int y = 0; y < gray.getHeight(); y++) {
Color color = new Color(gray.getRGB(x, y));
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
red = green = blue = (int)(red * 0.299 + green * 0.587 + blue * 0.114);
color = new Color(red, green, blue);
int rgb = color.getRGB();
gray.setRGB(x, y, rgb);
}
}
BufferedImage grayScale = ImageIO.read(new File("/path/to/file.bmp"));
// Automatic converstion....
ColorConvertOp op = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
op.filter(grayScale, grayScale);
add(new JLabel(new ImageIcon(master)));
add(new JLabel(new ImageIcon(gray)));
add(new JLabel(new ImageIcon(grayScale)));
} catch (IOException ex) {
Logger.getLogger(GrayScaleImage.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
现在,编写图像(上面没有演示)将非常简单...
ImageIO.write(grayScale, "BMP", new File("/path/to/grayscale file.bmp"));
// constant factors
final double GS_RED = 0.299;
final double GS_GREEN = 0.587;
final double GS_BLUE = 0.114;
现在检索每个元素,获取它的红色,蓝色,绿色成分和alpha(如果存在)并将它们乘以它们的对应因子。
R = G = B = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);
对于每个具有RGB值的像素,仅取R值并将其复制到G和B。这样每个像素将在3 RGB中包含红色值,这将导致图像变灰。 例。 数据[0] = 123 //红色数据[1] = 43 //绿色数据[2] = 78 //蓝色然后数据[0] = 123数据[1] = 123数据[2] = 123
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.