Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to flip vertical / mirror a bitmap using JAI ?

Posted on 2001-09-17
20
Medium Priority
?
1,393 Views
Last Modified: 2013-11-19
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
0
Comment
Question by:clo1
  • 10
  • 8
  • 2
20 Comments
 
LVL 2

Expert Comment

by:ia_ia_ia_1
ID: 6489795
This is a sample code, drawing a flipped image on a panel:
public class MyPanel extends JPanel {

    String filename = "D:/work/EE/dist2.gif";
    Image image;

    public MyPanel() {
        image = Toolkit.getDefaultToolkit().createImage(filename);
        setSize(image.getWidth(this), image.getHeight(this));
        setBackground(Color.green);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.rotate(Math.toRadians(90), getWidth()/2, getHeight()/2);
        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;
0
 
LVL 2

Author Comment

by:clo1
ID: 6489886
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 !!!
0
 
LVL 2

Author Comment

by:clo1
ID: 6489892
Because my java program ia an application, not a applet. So please provide the code for application. Thanks so much !!!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Expert Comment

by:yongsing
ID: 6490001
You can use JAI to do it:

// Imports
import javax.media.jai.*;
import java.awt.image.renderable.ParameterBlock;

// ...

// Load the image
String fileName = "images/Trees.jpg";
PlanarImage image = (PlanarImage)JAI.create("fileload", fileName);

// Flip the image vertically
ParameterBlock pb = new ParameterBlock();
pb.add(image);
pb.add(TransposeDescriptor.FLIP_VERTICAL);
RenderOp newImage = JAI.create("transpose", pb);

// Save the image
String outputFile = "images/Trees1.jpg";
JAI.create("filestore", newImage, outputFile, "jpeg", null);
0
 
LVL 2

Expert Comment

by:ia_ia_ia_1
ID: 6490068
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.jpg";
    Image image;

    public MyPanel() {
        image = Toolkit.getDefaultToolkit().createImage(filename);

        while (image.getHeight(null) == -1) {};
        setSize(image.getWidth(this), image.getHeight(this));
        setBackground(Color.green);

    }

    public BufferedImage flip() throws Exception {
        int size = Math.max(image.getHeight(this), image.getWidth(this));
        BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bi.createGraphics();
        g2.rotate(Math.toRadians(90), 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.createJPEGEncoder(out);
        JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
        param.setQuality(1.0f, true);
        encoder.setJPEGEncodeParam(param);
        encoder.encode(bi);

        out.close();
        g2.dispose();

        FileInputStream in = new FileInputStream(file);
        JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
        return decoder.decodeAsBufferedImage();

    }

    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.
0
 
LVL 9

Expert Comment

by:yongsing
ID: 6492144
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.ParameterBlock;

public class Test {

    public static void main(String[] args) {

        // Load the image
        String fileName = "update.jpg";
        PlanarImage image = (PlanarImage)JAI.create("fileload", fileName);

        // Flip the image vertically
        PlanarImage image1 = (PlanarImage)JAI.create("transpose", image, TransposeDescriptor.FLIP_VERTICAL);

        // Flip the image horizontally
        PlanarImage newImage = (PlanarImage)JAI.create("transpose", image1, TransposeDescriptor.FLIP_HORIZONTAL);

        // Save the image
        String outputFile = "update1.jpg";
        JAI.create("filestore", newImage, outputFile, "jpeg", null);
    }
}
0
 
LVL 2

Author Comment

by:clo1
ID: 6492254
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 !!!
0
 
LVL 9

Expert Comment

by:yongsing
ID: 6492283
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.
0
 
LVL 2

Author Comment

by:clo1
ID: 6492308
Yes. But my image is a bitmap. Would that be a problem? Cause I saw your coding is using a jpg file. Thanks
0
 
LVL 9

Expert Comment

by:yongsing
ID: 6492315
It shouldn't be a problem. JAI can read JPG, BMP, GIF, PNG etc files.

String fileName = "somefile.bmp";
PlanarImage image = (PlanarImage)JAI.create("fileload", fileName);

Have you tried it out?
0
 
LVL 2

Author Comment

by:clo1
ID: 6492332
Yes. But my image is a bitmap. Would that be a problem? Cause I saw your coding is using a jpg file. Thanks
0
 
LVL 9

Expert Comment

by:yongsing
ID: 6492339
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";
0
 
LVL 9

Expert Comment

by:yongsing
ID: 6492341
You can also save back the image in BMP format (version 3):

String outputFile = "somefile.bmp";
JAI.create("filestore", newImage, outputFile, "bmp", null);
0
 
LVL 2

Author Comment

by:clo1
ID: 6492347
Yes. But my image is a bitmap. Would that be a problem? Cause I saw your coding is using a jpg file. Thanks
0
 
LVL 2

Author Comment

by:clo1
ID: 6492348
Yes. But my image is a bitmap. Would that be a problem? Cause I saw your coding is using a jpg file. Thanks
0
 
LVL 9

Accepted Solution

by:
yongsing earned 1200 total points
ID: 6492364
Will you stop repeating the question and just try out my program? Here's how you read a BMP file, flip it, and then save back as a BMP file:

import javax.media.jai.*;
import javax.media.jai.operator.*;
import java.awt.image.renderable.ParameterBlock;

public class Test {

    public static void main(String[] args) {
        // Load the image
        String fileName = "somefile.bmp";
        PlanarImage image = (PlanarImage)JAI.create("fileload", fileName);

        // Flip the image vertically
        PlanarImage image1 = (PlanarImage)JAI.create("transpose", image, TransposeDescriptor.FLIP_VERTICAL);

        // Flip the image horizontally
        PlanarImage newImage = (PlanarImage)JAI.create("transpose", image1, TransposeDescriptor.FLIP_HORIZONTAL);

        // Save the image
        String outputFile = "somefile.bmp";
        JAI.create("filestore", newImage, outputFile, "bmp", null);
    }
}
0
 
LVL 2

Author Comment

by:clo1
ID: 6492382
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
0
 
LVL 2

Author Comment

by:clo1
ID: 6492384
Yes. But my image is a bitmap. Would that be a problem? Cause I saw your coding is using a jpg file. Thanks
0
 
LVL 2

Author Comment

by:clo1
ID: 6492397
It's work. Thanks !
0
 
LVL 9

Expert Comment

by:yongsing
ID: 6492405
Thanks for the points. Glad to be of help!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
In addition to being a great web-based presentation tool, Prezi also makes it easy to save your presentation as a PDF to share with others as well. Learn how in this tutorial. Select the share icon from the top menu in your Prezi editor: Select "D…
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question