473,586 Members | 2,863 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Writing JPEG file from pixel array of ints

3 New Member
Hello there, i am having the following problem, when i read an image the following way:

Expand|Select|Wrap|Line Numbers
  1. public void readBitmapImage (String imagePath){
  2.         ImageDir = imagePath;
  3.  
  4.         //Read in the image file into a BufferedImage object
  5.         BufferedImage img = null;
  6.         try {
  7.             img = ImageIO.read(new File(ImageDir));
  8.         } catch (IOException c) {
  9.             JOptionPane.showMessageDialog(null, c);
  10.         }
  11.  
  12.         //Create a raster and get the data from the image
  13.         Raster raster = img.getData();
  14.         //Get the images height and width
  15.         int height = raster.getHeight();
  16.         int width = raster.getWidth();
  17.         int size = height*width;
  18.         int [] pixels = new int[size * 3];
  19.         //Get the pixels from the image and populate the array with it
  20.         raster.getPixels(0, 0, width, height, pixels);
  21.  
  22.         imageData = pixels;
  23. }
  24.  
i get the correct values for the RGB pixels respectively, in the array of integers imageData. But when i try to create a new JPG file and write it with the sama data from the same array like this:
Expand|Select|Wrap|Line Numbers
  1. BufferedImage img = null;
  2.         try {
  3.             img = ImageIO.read(new File(ImageDir));
  4.         } catch (IOException c) {
  5.             JOptionPane.showMessageDialog(null, c);
  6.             return false;
  7.         }
  8.         //Create a raster and get the data from the image
  9.         Raster raster = img.getData();
  10.         //Get the images height and width
  11.         int height = raster.getHeight();
  12.         int width = raster.getWidth();
  13.         //Create a new image
  14.         BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  15.         //Create a raster in which to write to
  16.         WritableRaster raster2 = bi.getRaster();
  17.         //Set the pixels in the raster
  18.         raster2.setPixels(0, 0, width, height, imageData);
  19.         //Attach the raster to the image
  20.         bi.setData(raster2);
  21.  
  22.         String input = JOptionPane.showInputDialog(null, "Enter a name for the new image file");
  23.         File file = new File(DesDir +  "/" + input + ".jpg");
  24. //Write new file
  25.         try{
  26.         ImageIO.write(bi, "jpg", file);
  27.         }catch(IOException e){
  28.             JOptionPane.showMessageDialog(null, e);
  29.             return false;
  30.         }
  31.         progressBar.setString("done!");
  32.         return true;
  33.  
i get different values for the pixels in the new image, so the image is not the same as the original, can anyone tell me why? do i have to change the RGB values to YCbCr values or something before writing them to a file?
Thanks in advance
Feb 22 '08 #1
4 6634
BigDaddyLH
1,216 Recognized Expert Top Contributor
I asked you on Sun's Java forums and I'm still not sure what you're trying to do. If you are trying to copy a file, why not copy it byte-by-byte? I'm also not clear on what the error is. Is the image corrupted or is it just small differences (due to compression) that don't reveal themselves to the eye. Please try to be clear.
Feb 22 '08 #2
cagdasal
3 New Member
here for example, in the original image, the first pixel has the values (top left of image): 0, 103, 220 RGB, in the new image that was created from the array of integers holding the RGB pixel values of the original image (from the code above) has the value: 26, 78, 255 RGB respectively.

Another example, the last pixel in the original image has the values : 51, 139, 236, in the new image it has : 68, 136, 217

Why is this, any ideas?
Feb 22 '08 #3
cagdasal
3 New Member
The filesize decreases when an image is created aswell, so i guess its because of compression? if so any ideas on a workaround?
Feb 22 '08 #4
BigDaddyLH
1,216 Recognized Expert Top Contributor
Are you trying to make the image smaller?
Feb 22 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
10193
by: Patrick | last post by:
Hello all! I am using a BufferedImage object to build an image from scratch. I want it to be a grayscale image with only 8bits of color. I have the color information as a byte, saved in variable name byte4. I have written the following code: First, thePanorama is defined as: new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);...
10
21463
by: Caryn Graves | last post by:
I need to write hex into a binary file, i.e., '00A8' (decimal 168) output as 2 bytes, 'FE' (decimal 254) output as 1 byte, etc. Suggestions??
2
3988
by: User10 | last post by:
Can some one provide an algorithm for motion detection between two jpeg frames? Or can you provide a more appropriate group to post this on? Thanks!
5
4723
by: TheGanjaMan | last post by:
Hi everyone, I'm trying to write up a simple image stamper application that stamps the Exif date information from the jpegs that I've taken from my digital camera and saves the new file with the date stamped on the lower right part of the picture. (I'm not an advanced programmer so my code may not be 100% efficient - sorry, I'm still...
8
5420
by: Skeleton Man | last post by:
Hi guys, I was thinking.. wouldn't it be cool if you could take an image (jpeg) and redraw the entire thing in HTML ? In theory you could parse the image to create an array containing the RGB value for every pixel and then print this out as a series of 1 pixel wide DIV's with the background color set to the pixel colour. I realise this...
6
1908
by: William Gill | last post by:
I have found a couple of sites that allow a visitor to upload an image and the site returns either the "average color", or a palette of colors. Several of them use PHP to accomplish this. I have requested the source code, but have not gotten a response (even though one site states "source code available on request"). It is obvious that they...
2
2247
by: devnew | last post by:
hi i am new to python and PIL and was trying to write a class to read and write RGB color model jpeg images.. i came up with this class below..i want to know if this is the way to read/write the RGB color model jpeg..if anyone can give feedback (esp on the methods writeImage1( ) and writeImage2( ) it wd help my studies .. TIA dn
1
3240
by: Joe Cool | last post by:
I am attempting to add a function to an application I am working on to modify the JPEG Comment in a Jpeg image file. I can retrieve the JPEG Comment with no problem. The problem is modifying it. I have the contents of a Jpeg loaded into an Image object, _Image, using the Image.FromFile method. I convert the Text property of a TextBox...
2
4473
by: minouparhizkar | last post by:
hi could anyone helping me to finish this i have no idea how to implement the program in java im trying to grabbing the pixel from image and then convert it to the txt file .i did that but it didnt give me back any result could you tell me whats wrong with that. here is the pixel grabber file: package digitalpen; import...
0
7911
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8200
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.