Sunday, January 8, 2012

Flipping an Image

Flipping an image means mirroring it on a certain axis. Flipping vertically could be compared to the reflection of yourself in a lake, flipping horizontally is kind of like looking into a mirror. To do a flip, we use drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer); of Graphics2D object. This method lets us draw part of an image into an image, or scale an image etc. The method takes an image, and a set of coordinates of the destination rectangle, it also takes a set of coordinates for the source rectangle of the image you supply. These source coordinates can be manipulated for varying results, as I will show in this next code segment.

For flipping an image horizontally, we give the source rectangle coordinates starting from the top right, and ending at the bottom left like this.
drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer);
g.drawImage(The Source Image, 0, 0, image width, image height, image width, 0, 0, image height, null);
as opposed to the unflipped way
g.drawImage(The Source Image, 0, 0, image width, image height, 0, 0, image width, image height, null);
public static BufferedImage horizontalflip(BufferedImage img) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage dimg = new BufferedImage(w, h, img.getType());
Graphics2D g = dimg.createGraphics();
g.drawImage(img, 0, 0, w, h, w, 0, 0, h, null);
g.dispose();
return dimg;
}
Running Codes

Making a Certain Color Transparent

A useful function of many imaging programs such as GIMP is its ability to make only one color of an Image transparent; this is called applying a mask. To do this, GIMP evaluates each pixel of an image, and tests if it is the user specified masking color, if it is it sets that pixel's RGB value to nothing, and it's alpha to 0.0. Here is the Java implementation:
public static BufferedImage makeColorTransparent(String ref, Color color) {
BufferedImage image = loadImage(ref);
BufferedImage dimg = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = dimg.createGraphics();
g.setComposite(AlphaComposite.Src);
g.drawImage(image, null, 0, 0);
g.dispose();
for(int i = 0; i < dimg.getHeight(); i++) {
for(int j = 0; j < dimg.getWidth(); j++) {
if(dimg.getRGB(j, i) == color.getRGB()) {
dimg.setRGB(j, i, 0x8F1C1C);
}
}
}
return dimg;
}
This grabs the Graphics2D instance of the destination image, and sets it's composite to Alpha. It then draws the Image onto the screen, and disposes of the Graphics2D to clear up system resources. Next, it cycles through all the pixels in the image, using the for loops, and compares the color of the pixel to the mask color. If the pixels color matches the mask color, that pixels color is replaced by an alpha RGB value. Lastly, the destination image, with mask applied, is returned.
Running Codes

Making an Image Translucent

public static BufferedImage loadTranslucentImage(String url, float transperancy) {
// Load the image
BufferedImage loaded = loadImage(url);
// Create the image using the
BufferedImage aimg = new BufferedImage(loaded.getWidth(), loaded.getHeight(), BufferedImage.TRANSLUCENT);
// Get the images graphics
Graphics2D g = aimg.createGraphics();
// Set the Graphics composite to Alpha
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transperancy));
// Draw the LOADED img into the prepared reciver image
g.drawImage(loaded, null, 0, 0);
// let go of all system resources in this Graphics
g.dispose();
// Return the image
return aimg;
}
Running Codes

Saving Image

public static void saveOldWay(String ref, BufferedImage img) {
BufferedOutputStream out;
try {
out = new BufferedOutputStream(new FileOutputStream(ref));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(img);
int quality = 5;
quality = Math.max(0, Math.min(quality, 100));
param.setQuality((float) quality / 100.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(img);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Running Codes

Creating and Using your own Image in Java

public class ImageMaker {
public static BufferedImage createImage() {
BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
img.createGraphics();
Graphics2D g = (Graphics2D)img.getGraphics();
g.setColor(Color.YELLOW);
g.fillRect(0, 0, 100, 100);
for(int i = 1; i < 49; i++) {
g.setColor(new Color(5*i, 5*i, 4+1*2+i*3));
g.fillRect(2*i, 2*i, 3*i, 3*1);
}
return img;
}
public static void main(String[] args) {
JFrame frame = new JFrame("Image Maker");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
});
frame.setBounds(0, 0, 200, 200);
JImagePanel panel = new JImagePanel(createImage(), 50, 45);
frame.add(panel);
frame.setVisible(true);
}
}
Running Codes

Drawing an Image on the Screen using Graphics2D

public class ImageApp {
public void loadAndDisplayImage(JFrame frame) {
// Load the img
BufferedImage loadImg = ImageUtil.loadImage("C:/Images/duke.gif");
frame.setBounds(0, 0, loadImg.getWidth(), loadImg.getHeight());
// Set the panel visible and add it to the frame
frame.setVisible(true);
// Get the surfaces Graphics object
Graphics2D g = (Graphics2D)frame.getRootPane().getGraphics();
// Now draw the image
g.drawImage(loadImg, null, 0, 0);
}
public static void main(String[] args) {
ImageApp ia = new ImageApp();
JFrame frame = new JFrame("Tutorials");
ia.loadAndDisplayImage(frame);
}
}
Running Codes

Using Blocking Queues


package com.oreilly.tiger.ch10;


import java.io.PrintStream;

import java.util.Date;

import java.util.concurrent.BlockingQueue;


public class Producer extends Thread {


private BlockingQueue q;

private PrintStream out;


public Producer(BlockingQueue q, PrintStream out) {

setName("Producer");
this.q = q;

this.out = out;

}



public void run( ) {

try {

while (true) {

q.put(produce( ));

}

} catch (InterruptedException e) {

out.printf("%s interrupted: %s", getName( ), e.getMessage( ));

}

}


private String produce( ) {

while (true) {

double r = Math.random( );



// Only goes forward 1/10 of the time

if ((r*100) < 10) {

String s = String.format("Inserted at %tc", new Date( ));

return s;

}

}

}

}