Well, i got this.. cause in my place they request it like that.. Well i have this: //some variables and coments are a possible code.. But i mean i don't know how to do it exactly, i found on internet that to convert R is the byte is in *0.21 with G is *0.71 i guess and blue is * 0.07. I just can use that library java.io.*; and i'm working with BMP format images of 1024 x 768, if you can help me, or you want me to be more specific please tell me. i don't know if i have to add another variables or to take some out, or to modify, please help me. I'm not so new in this language, but i'm not an expert. The code is right here:
import java.io.*;
public class Grayscale{
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);
}
}
Now retrieve each and gett it's red, blue, green component and alpha if exist and multiply them with their corresponding factor.
Most image formats have header information before the pixel data, so when you're reading these types of files, you need to take that into consideration...
Frankly, it's much easier to rely on pre-existing library where you can.
ImageIO
allows you to read and write a number of different file formats, including BMP.Take a look at
The next decision is - do you convert the image yourself or use a pre-existing filter. You'll have to do some of your own metrics, but in the past, I've found pixel manipulation of an image to be slow, slower than the in built filters at the very least...
Original, manual grayscale, automatical/filter grayscale
Now, writing the image (which is not demonstrated above) would be as simple as...
For each pixel you have the RGB values, take only the R value and copy it to G and B. So that each pixel will contain the red value in the 3 RGB this will cause the image to be gray. Example. data [0]=123 //red data [1]=43 //Green data [2]=78 //blue Then data [0]=123 data [1]=123 data [2]=123