R
royG
hi
i was learning some image manipulation using JDK's imageio classes.I
am extracting the imagedata as int[] using getRGB() and then using
that int[] ,i want to create a new image.
import java.awt.image.*;
import java.io.File;
public class ImageDemo {
public static void main(String[] args)throws Exception {
String inputFile="F:\\myimgs\\grayscale.png";
BufferedImage img = javax.imageio.ImageIO.read(new File(inputFile));
int ht=img.getHeight();
int wd=img.getWidth();
int numpixels=ht*wd;
int[] imgdataints=new int[numpixels];
img.getRGB( 0, 0, wd, ht,imgdataints, 0, wd);
String newimage="F:\\myimgs\\newimage.jpg";
writeImage(newimage,imgdataints,wd);
}
public static void writeImage(String imgname,int[] data,int wd)throws
Exception{
BufferedImage bufimg=new BufferedImage(wd,data.length/wd,10);
Raster rast=bufimg.getData();
WritableRaster wr= rast.createCompatibleWritableRaster();
wr.setPixels(0, 0, wd,data.length/wd, data);
bufimg.setData(wr);
File newfile=new File(imgname);
javax.imageio.ImageIO.write(bufimg, "jpg", newfile);
}
}
I am getting a newimage,jpg created.But it looks a too bright unlike
the original image.Why does this happen?I am using the int[] extracted
from getRGB() so should i not get the exact image?
Is there anything i need to recreate the original image,with the same
brightness ?
thanks
roy
i was learning some image manipulation using JDK's imageio classes.I
am extracting the imagedata as int[] using getRGB() and then using
that int[] ,i want to create a new image.
import java.awt.image.*;
import java.io.File;
public class ImageDemo {
public static void main(String[] args)throws Exception {
String inputFile="F:\\myimgs\\grayscale.png";
BufferedImage img = javax.imageio.ImageIO.read(new File(inputFile));
int ht=img.getHeight();
int wd=img.getWidth();
int numpixels=ht*wd;
int[] imgdataints=new int[numpixels];
img.getRGB( 0, 0, wd, ht,imgdataints, 0, wd);
String newimage="F:\\myimgs\\newimage.jpg";
writeImage(newimage,imgdataints,wd);
}
public static void writeImage(String imgname,int[] data,int wd)throws
Exception{
BufferedImage bufimg=new BufferedImage(wd,data.length/wd,10);
Raster rast=bufimg.getData();
WritableRaster wr= rast.createCompatibleWritableRaster();
wr.setPixels(0, 0, wd,data.length/wd, data);
bufimg.setData(wr);
File newfile=new File(imgname);
javax.imageio.ImageIO.write(bufimg, "jpg", newfile);
}
}
I am getting a newimage,jpg created.But it looks a too bright unlike
the original image.Why does this happen?I am using the int[] extracted
from getRGB() so should i not get the exact image?
Is there anything i need to recreate the original image,with the same
brightness ?
thanks
roy