clo1
asked on
How to flip vertical / mirror a bitmap using JAI ?
I have a bitmap file (just black and white), and I would like to use JAI (Java Advanced Imaging) or anything from java to flip the bitmap vertically or mirror it. Please give me sample code on how to do it. By the way, my java is an application, not an applet. Please kindly provide the code for app. instead of an applet. Thanks !!
PS... IT IS A "BITMAP" image
PS... IT IS A "BITMAP" image
ASKER
Thanks and appreciate for your sample code. But is there any code which can let me save it back to a file instead of showing in the panel?? I will give you points when you have post it. Thanks again !!!
ASKER
Because my java program ia an application, not a applet. So please provide the code for application. Thanks so much !!!
You can use JAI to do it:
// Imports
import javax.media.jai.*;
import java.awt.image.renderable. ParameterB lock;
// ...
// Load the image
String fileName = "images/Trees.jpg";
PlanarImage image = (PlanarImage)JAI.create("f ileload", fileName);
// Flip the image vertically
ParameterBlock pb = new ParameterBlock();
pb.add(image);
pb.add(TransposeDescriptor .FLIP_VERT ICAL);
RenderOp newImage = JAI.create("transpose", pb);
// Save the image
String outputFile = "images/Trees1.jpg";
JAI.create("filestore", newImage, outputFile, "jpeg", null);
// Imports
import javax.media.jai.*;
import java.awt.image.renderable.
// ...
// Load the image
String fileName = "images/Trees.jpg";
PlanarImage image = (PlanarImage)JAI.create("f
// Flip the image vertically
ParameterBlock pb = new ParameterBlock();
pb.add(image);
pb.add(TransposeDescriptor
RenderOp newImage = JAI.create("transpose", pb);
// Save the image
String outputFile = "images/Trees1.jpg";
JAI.create("filestore", newImage, outputFile, "jpeg", null);
I'm sorry I'm not familiar with JAI.
This is what I could think of so far:
import javax.swing.JPanel;
import java.awt.*;
import java.io.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.* ;
public class MyPanel extends JPanel {
String filename = "D:/work/EE/AskAQuestion.j pg";
Image image;
public MyPanel() {
image = Toolkit.getDefaultToolkit( ).createIm age(filena me);
while (image.getHeight(null) == -1) {};
setSize(image.getWidth(thi s), image.getHeight(this));
setBackground(Color.green) ;
}
public BufferedImage flip() throws Exception {
int size = Math.max(image.getHeight(t his), image.getWidth(this));
BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB );
Graphics2D g2 = bi.createGraphics();
g2.rotate(Math.toRadians(9 0), size/2, size/2);
g2.drawImage(image, 0, 0, image.getWidth(this), image.getHeight(this), null);
File file = new File("test.jpg");
FileOutputStream out = new FileOutputStream(file);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncode r(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEnco deParam(bi );
param.setQuality(1.0f, true);
encoder.setJPEGEncodeParam (param);
encoder.encode(bi);
out.close();
g2.dispose();
FileInputStream in = new FileInputStream(file);
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecode r(in);
return decoder.decodeAsBufferedIm age();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
try {
g2.drawImage(flip(), 0, 0, getWidth(), getHeight(), this);
} catch (Exception e) {
e.printStackTrace();
}
}
}
There are still some minor issues related to the size of the image. If u would like to use the approach, I'll fix them.
This is what I could think of so far:
import javax.swing.JPanel;
import java.awt.*;
import java.io.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*
public class MyPanel extends JPanel {
String filename = "D:/work/EE/AskAQuestion.j
Image image;
public MyPanel() {
image = Toolkit.getDefaultToolkit(
while (image.getHeight(null) == -1) {};
setSize(image.getWidth(thi
setBackground(Color.green)
}
public BufferedImage flip() throws Exception {
int size = Math.max(image.getHeight(t
BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB
Graphics2D g2 = bi.createGraphics();
g2.rotate(Math.toRadians(9
g2.drawImage(image, 0, 0, image.getWidth(this), image.getHeight(this), null);
File file = new File("test.jpg");
FileOutputStream out = new FileOutputStream(file);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncode
JPEGEncodeParam param = encoder.getDefaultJPEGEnco
param.setQuality(1.0f, true);
encoder.setJPEGEncodeParam
encoder.encode(bi);
out.close();
g2.dispose();
FileInputStream in = new FileInputStream(file);
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecode
return decoder.decodeAsBufferedIm
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
try {
g2.drawImage(flip(), 0, 0, getWidth(), getHeight(), this);
} catch (Exception e) {
e.printStackTrace();
}
}
}
There are still some minor issues related to the size of the image. If u would like to use the approach, I'll fix them.
The example below is a working program using JAI to read an image, flip it vertically and horizontally, and then save it.
import javax.media.jai.*;
import javax.media.jai.operator.* ;
import java.awt.image.renderable. ParameterB lock;
public class Test {
public static void main(String[] args) {
// Load the image
String fileName = "update.jpg";
PlanarImage image = (PlanarImage)JAI.create("f ileload", fileName);
// Flip the image vertically
PlanarImage image1 = (PlanarImage)JAI.create("t ranspose", image, TransposeDescriptor.FLIP_V ERTICAL);
// Flip the image horizontally
PlanarImage newImage = (PlanarImage)JAI.create("t ranspose", image1, TransposeDescriptor.FLIP_H ORIZONTAL) ;
// Save the image
String outputFile = "update1.jpg";
JAI.create("filestore", newImage, outputFile, "jpeg", null);
}
}
import javax.media.jai.*;
import javax.media.jai.operator.*
import java.awt.image.renderable.
public class Test {
public static void main(String[] args) {
// Load the image
String fileName = "update.jpg";
PlanarImage image = (PlanarImage)JAI.create("f
// Flip the image vertically
PlanarImage image1 = (PlanarImage)JAI.create("t
// Flip the image horizontally
PlanarImage newImage = (PlanarImage)JAI.create("t
// Save the image
String outputFile = "update1.jpg";
JAI.create("filestore", newImage, outputFile, "jpeg", null);
}
}
ASKER
yongsing, I have tried, but I can even get this to run. You know, what I want to flip is a bitmap image, would that be the cause of the problem? Please help me again. Thanks a lot !!!
Did you try my code? I have tried it and it is working fine for me here.
Are you using JAI? You need to download the software and set the classpath to get it to work.
Are you using JAI? You need to download the software and set the classpath to get it to work.
ASKER
Yes. But my image is a bitmap. Would that be a problem? Cause I saw your coding is using a jpg file. Thanks
It shouldn't be a problem. JAI can read JPG, BMP, GIF, PNG etc files.
String fileName = "somefile.bmp";
PlanarImage image = (PlanarImage)JAI.create("f ileload", fileName);
Have you tried it out?
String fileName = "somefile.bmp";
PlanarImage image = (PlanarImage)JAI.create("f
Have you tried it out?
ASKER
Yes. But my image is a bitmap. Would that be a problem? Cause I saw your coding is using a jpg file. Thanks
No, it is not a problem. Will you please try it out? Just subsitute the file name for your bitmap file:
String fileName = "somefile.bmp";
String fileName = "somefile.bmp";
You can also save back the image in BMP format (version 3):
String outputFile = "somefile.bmp";
JAI.create("filestore", newImage, outputFile, "bmp", null);
String outputFile = "somefile.bmp";
JAI.create("filestore", newImage, outputFile, "bmp", null);
ASKER
Yes. But my image is a bitmap. Would that be a problem? Cause I saw your coding is using a jpg file. Thanks
ASKER
Yes. But my image is a bitmap. Would that be a problem? Cause I saw your coding is using a jpg file. Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I didn't repeat the question. It's the Experts-exchange problem. I have tried. And it stop on the first line of your code. I donno why, but I am examing the problem now. I think it's the absent of the jai_cors.jar. I will get back to you later..Thanks
ASKER
Yes. But my image is a bitmap. Would that be a problem? Cause I saw your coding is using a jpg file. Thanks
ASKER
It's work. Thanks !
Thanks for the points. Glad to be of help!
public class MyPanel extends JPanel {
String filename = "D:/work/EE/dist2.gif";
Image image;
public MyPanel() {
image = Toolkit.getDefaultToolkit(
setSize(image.getWidth(thi
setBackground(Color.green)
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.rotate(Math.toRadians(9
g2.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
}
As u could see, the flipping is done by the rotate method of Graphics2D. If you are not using Java2, tell me and I'll try to work out it without this cast: Graphics2D g2 = (Graphics2D) g;