Hello there, i am having the following problem, when i read an image the following way: -
public void readBitmapImage (String imagePath){
-
ImageDir = imagePath;
-
-
//Read in the image file into a BufferedImage object
-
BufferedImage img = null;
-
try {
-
img = ImageIO.read(new File(ImageDir));
-
} catch (IOException c) {
-
JOptionPane.showMessageDialog(null, c);
-
}
-
-
//Create a raster and get the data from the image
-
Raster raster = img.getData();
-
//Get the images height and width
-
int height = raster.getHeight();
-
int width = raster.getWidth();
-
int size = height*width;
-
int [] pixels = new int[size * 3];
-
//Get the pixels from the image and populate the array with it
-
raster.getPixels(0, 0, width, height, pixels);
-
-
imageData = pixels;
-
}
-
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: -
BufferedImage img = null;
-
try {
-
img = ImageIO.read(new File(ImageDir));
-
} catch (IOException c) {
-
JOptionPane.showMessageDialog(null, c);
-
return false;
-
}
-
//Create a raster and get the data from the image
-
Raster raster = img.getData();
-
//Get the images height and width
-
int height = raster.getHeight();
-
int width = raster.getWidth();
-
//Create a new image
-
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
-
//Create a raster in which to write to
-
WritableRaster raster2 = bi.getRaster();
-
//Set the pixels in the raster
-
raster2.setPixels(0, 0, width, height, imageData);
-
//Attach the raster to the image
-
bi.setData(raster2);
-
-
String input = JOptionPane.showInputDialog(null, "Enter a name for the new image file");
-
File file = new File(DesDir + "/" + input + ".jpg");
-
//Write new file
-
try{
-
ImageIO.write(bi, "jpg", file);
-
}catch(IOException e){
-
JOptionPane.showMessageDialog(null, e);
-
return false;
-
}
-
progressBar.setString("done!");
-
return true;
-
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
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.
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?
The filesize decreases when an image is created aswell, so i guess its because of compression? if so any ideas on a workaround?
BigDaddyLH 1,216
Recognized Expert Top Contributor
Are you trying to make the image smaller?
Sign in to post your reply or Sign up for a free account.
Similar topics |
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);...
|
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??
|
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!
|
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...
|
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...
| |
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...
|
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
|
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...
|
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...
|
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...
|
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. ...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |