Hi,
I created an image using random numbers pixels, i.e. each pixel consists of three random numbers one for green color, another for the blue color and the last for the red color. I stored the image to a jpg file and I write a code to read and retreive the RGB values from the image, but the problem is that I retreved values that does not match the original values(when the image created). Here is the code for writing to and reading from a jpg file:
1. Writing to an image file
.// fill the array pixels with random numbers . . // writing to a file private JLabel createImageLabel(int[] pixels) { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); WritableRaster raster = image.getRaster(); raster.setPixels(0, 0, width, height, pixels); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int[] data = new int[3]; System.out.println("x,y: " + j + ", " + i); raster.getPixel(j, i, data); for (int p=0;p<=data.length-1;p++) { System.out.println(data[p]+" "); } }} JLabel label = new JLabel( new ImageIcon(image) ); try{ File f = new File("d:\\test.jpg"); ImageIO.write(image,"jpeg",f ); } catch(IOException e){System.out.println(e);} return label; } 2. Reading from an image public ReadImage() { try{ BufferedImage image = ImageIO.read(new File( "d:\\test.jpg" )); int w = image.getWidth(); int h = image.getHeight(); for (int i=0; i<h; i++){ for (int j=0; j<w; j++){ int pixel = image.getRGB(j, i); int alpha = (pixel >> 24) & 0xff; int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel) & 0xff; System.out.println("argb: " + alpha + ", " + red + ", " + green + ", " + blue); }} } catch(IOException e){System.out.println(e);}
.
I greately appreciate your help.
Regards,
Ahmad