Rotation by AffineTransform makes saved image empty - java

Ok my problem is simple, after performing AffineTransform, my image is not saving properly (however it is drawn on JPanel properly!). It is really strange, so any hints are really appreciated...
Take a look on code:
public BufferedImage performRotation(BufferedImage bi){
if (angle!=180){
at.translate(0.5*bi.getHeight(), 0.5*bi.getWidth());
if(clockwise){
at.rotate(Math.toRadians(angle));
}else{
at.rotate(Math.toRadians(-angle));
}
at.translate(-0.5*bi.getWidth(), -0.5*bi.getHeight());
}
else if(angle==180){
at.translate(0.5*bi.getWidth(), 0.5*bi.getHeight());
at.rotate(Math.toRadians(angle));
at.translate(-0.5*bi.getWidth(), -0.5*bi.getHeight());
}
AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
BufferedImage bi2 = op.filter(bi, null);
try {
ImageIO.write(bi, "bmp", new File("BEFORE filterORIG.bmp"));
ImageIO.write(bi2, "bmp", new File("AFTER filterNEW.bmp"));
} catch (IOException ex) {
Logger.getLogger(DrawingField.class.getName()).log(Level.SEVERE, null, ex);
}
File BEFORE filterORIG is saved properly -> there is an image, but its pre-rotated.
File AFTER... is saved as blank file.
What is really interesting, is previously mentioned fact that this transformation is poperly shown on JPanel that i use as a display (i can observe effect of desired transformation)
Any help appreciated...

Try writing png images, ie:
ImageIO.write(bi, "png", new File("BEFORE filterORIG.png"));
ImageIO.write(bi2, "png", new File("AFTER filterNEW.png"));
The resulting image (bi2) may have an aplha channel and ImageIO may not allow to encode images with aplha as bmp.
Alternatively, create a destination image with TYPE_INT_RGB color model and use it as a second argument in filter() method.

Related

BufferedImageOp Kernel Convolution not doing anything

I am attempting to perform a basic kernel convolution pass on an image using the BufferedImageOp package in java.awt.image. This is the code I have:
BufferedImage img = null;
File f = null;
//read image
try {
f = new File("keys.JPG");
img = ImageIO.read(f);
} catch (IOException e) {
System.out.println(e);
}
float[] gaussian = {
1/16f, 1/8f, 1/16f,
1/8f, 1/4f, 1/8f,
1/16f, 1/8f, 1/16f,
};
BufferedImageOp op = new ConvolveOp(new Kernel(3, 3, gaussian));
BufferedImage dest = op.filter(img, null);
File outputfile = new File("image.jpg");
ImageIO.write(dest, "jpg", outputfile);
My code attempts to load the image keys.JPG and then convolve this image with the Gaussian blur kernel and save the image to the file image.jpg. When I run the code, it processes for a bit then terminates and saves the image successfully but when I compare the original and the new images, they are identical.
Looking online at some code examples, my code should work. Am I missing something?
Thanks
As #haraldK mentioned, my image was too large to notice a difference. The code works as expected.

javafx read javafx.scene.image.Image to ImageIO.write(), from CMYK to RGB

What I want to do is read an image from FileChooser and write it to file. I had to store the image in a javafx.scene.image.Image so that I can display it and clip it inside a circle. I have a little problem with trying to write the image that I got from javafx.scene.image.Image to file. The conversion process is not fluid, converts from CMYK to RGB (therefore turning my picture to some pink thing.
Please, I have checked a lot of other sources, and no one has been able to give me a notable solution
FileChooser fileChooser = new FileChooser();
File selectedFile = fileChooser.showOpenDialog(parent);
// get Image from selectedFile
Image userImg = = new Image( selectedFile.toURI().toURL().toString() );
if ( userImg != null ) {
String format = "jpg";
String filename = "d:\\pictureName."+ format;
File file = new File(filename);
// convert Image to BufferedImage
BufferedImage bufferedImage = SwingFXUtils.fromFXImage( userImg, null);
try {
// this is where i want to convert the color mode
// from cmyk to rgb, before i write it to file
ImageIO.write( bufferedImage, format, file );
} catch (IOException e) {
System.out.println("Exception :: "+ e.getMessage() );
}
}
Why do you think that there is some CMYK to RGB conversion happening? I suspect the reason for your "pink thing" is something different. The easiest way to find out is to change your output format from jpeg to png and see whether it makes a difference.
I think you are again one of the many people who are hit by this bug https://bugs.openjdk.java.net/browse/JDK-8119048 which is not considered important enough to be fixed. If you read the comments in there you will find a work-arround. Basically the idea is to copy the image after the conversion into a new image without alpha channel.
I'd really like to know how many more people have to waste their time until this bug finally gets enough attention to be fixed.

How to set PIxelFormat to rgb in javafx?

With javafx, when I read images the PixelFormat is rgb and I want to apply filters on them.I use writableImage.pixelWriter to set the pixels color but the PixelFormat is rgba.
I use ImageIO.write() and it work for .png, but when I try to save this modified image in .jpg or .jpeg, the color change. I find that's because rgba don't work with jpg but i don't know how to change this PixelFormat.
Is there a way to change rgba format in rgb ? Do you know another way to save javafx Image as .jpg/.jpeg ?
Edit : How I save file (it works for png)
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("JPG Files", "*.jpg"));
File outputFile = fileChooser.showSaveDialog(null);
BufferedImage bImage = SwingFXUtils.fromFXImage(modifiedImage.getImage(), null); // getImage() return a javafx.scene.image.Image;
try {
ImageIO.write(bImage, "jpg", outputFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
How I modify image (example)
Image currentImage = modifiedImage.getImage();
WritableImage writableImage = new WritableImage((int) currentImage.getWidth(), (int) currentImage.getHeight());
PixelReader pixelReader = currentImage.getPixelReader();
PixelWriter pixelWriter = writableImage.getPixelWriter();
for (int i = 0; i < (int) currentImage.getWidth(); i++) {
for (int j = 0; j < (int) currentImage.getHeight(); j++) {
pixelWriter.setColor(i, j, new Color(0,0,0,1));
}
}
modifiedImage.setImage(writableImage);
When I get the PixelFormat of the reader it's rgb but for the writer it's rgba. If I save an image without modification it's good, but when I apply a filter on it and I save the image as jpg, the colors change.
On my app the colors are good but if I open the jpg file outside they aren't. With png files there is no problem. I can allow to save only as png but it would be better if i can choose.
Solution :
BufferedImage bImage = SwingFXUtils.fromFXImage(modifiedImage.getImage(), null);
BufferedImage bImage2 = new BufferedImage(bImage.getWidth(), bImage.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
bImage2.getGraphics().drawImage(bImage, 0, 0, null);
try {
ImageIO.write(bImage2, "jpg", outputFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
You are again one of the many people who are hit by this bug https://bugs.openjdk.java.net/browse/JDK-8119048 which is not considered important enough to be fixed. If you read the comments in there you will find a work-arround. Basically the idea is to copy the image after the conversion into a new image without alpha channel.
I'd really like to know how many more people have to waste their time until this bug finally gets enough attention to be fixed.

Java display base 64 byte array in jsp

I have a class called Graphic that creates a new BufferedImage, draws a new Graphics2D and returns this image as base64 encoded String:
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Draw background
g2.setColor(Color.decode("#FFFFFF"));
g2.fillRect(0, 0, grafikBreite, grafikHoehe);
g2.setColor(Color.decode("#000000"));
// Draw some rectangles and other stuff...
drawStuff(g2);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] imageInByte = {};
try {
JPEGImageEncoder j = new JPEGImageEncoderImpl(baos);
j.encode(image);
imageInByte = baos.toByteArray();
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
return javax.xml.bind.DatatypeConverter.printBase64Binary(imageInByte);
In my jsp-File I want to display this image using, where graphic is the previously created base64 byte array:
<img src="data:image/jpg;base64,<c:out value="${graphic}"/>"/>
The image is displayed, but the problem is that the image has red background and the other colors used are also wrong. If I save the created base64 string as jpeg-File on hard disk, all colors are displayed correctly.
Has someone an idea why HTML displays the image with strange colors?
Thanks for help
First a bit cleaning up:
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, grafikBreite, grafikHoehe);
g2.setColor(Color.BLACK);
drawStuff(g2);
g2.dispose(); // TODO
Dispose after createGraphics.
Then one could try the more generic, portable ImageIO class. The parametrising for antialiasing and such goes a bit different, but then JPEG is a lossy format anyway. Just to try a different angle.
ImageIO.write(image, "jpg", baos);
baos.close();
imageInByte = baos.toByteArray();
And then I did the closing first. (It has no effect by javadoc.)
One could try .png and a another type, ABGR.
I think ImageIO does the trick, or your code with ABGR.

Java 1.5.0_16 corrupted colours when saving jpg image

i have a loaded image from disk (stored as a BufferedImage), which i display correctly on a JPanel but when i try to re-save this image using the command below, the image is saved in a reddish hue.
ImageIO.write(image, "jpg", fileName);
Note! image is a BufferedImage and fileName is a File object pointing to the filename that will be saved which end in ".jpg".
I have read that there were problems with ImageIO methods in earlier JDKs but i'm not on one of those versions as far as i could find. What i am looking for is a way to fix this issue without updating the JDK, however having said that i would still like to know in what JDK this issue was fixed in (if it indeed is still a bug with the JDK i'm using).
Thanks.
Ok, solved my problem, it seems that i need to convert the image to BufferedImage.TYPE_INT_RGB for some reason. I think the alpha channels might not be handled correctly at some layer.
I would first start by investigating if it is the BifferedImage color model that is the problem or the jpeg encoding. You could try changing the image type (3rd argument in the constructor), to see if that produces a difference, and also use the JPEGCodec directly to save the jpeg.
E.g.
BufferedImage bufferedImage = ...; // your image
out = new FileOutputStream ( filename );
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder ( out );
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam ( bufferedImage );
encoder.setJPEGEncodeParam ( param );
encoder.encode ( bufferedImage );
out.close();
EDIT: changed text, it's the image type you want to change. See the link to the constructor.
Another approach is to render the image in a TYPE_INT_ARGB buffer, has a DirectColorModel with alpha, as outlined below and suggested here.
private BufferedImage process(BufferedImage old) {
int w = old.getWidth();
int h = old.getHeight();
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.drawImage(old, 0, 0, null);
g2d.dispose();
return img;
}

Categories