How can I get my image to flip? - java

I'm having a problem getting an image to flip. My program is supposed to show the default image and the flipped image. I thought that if I could replace the (0,0) pixel of the flipped picture with the (width-1,height-1) of the original picture, it would work, but instead of getting the original image, I get this.
Here is my code:
import java.awt.Color;
public class Horizontal {
public static void main(String[] args)
{
Picture source = new Picture(args[0]);//name of picture.
Picture flip = new Picture(source.width(), source.height());//sets the width and height of source
for (int i =0; i < source.width(); i++)
{
int w = 1;
int sw = source.width()-w;
for (int j = 0; j < source.width(); j++)
{
int h=1;
int sh = source.height()-h;
Color SourceColor = source.get(sw,sh);// return the the color pixel of (sw,sh)
flip.set(i, j, SourceColor);//suppose to replace the (i,j) pixel of flip with source's (sw,sh) pixel
h++;
}
w++;
}
source.show();// shows the original image
flip.show(); // shows flipped version of image
}
}

Chceck this site. It has great info about basic image algorithms in java.
You can copy code for flipping an image.
http://www.javalobby.org/articles/ultimate-image/#9
Flipping horizontally:
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;
}
Flipping vertically:
public static BufferedImage verticalflip(BufferedImage img) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage dimg = dimg = new BufferedImage(w, h, img.getColorModel().getTransparency());
Graphics2D g = dimg.createGraphics();
g.drawImage(img, 0, 0, w, h, 0, h, w, 0, null);
g.dispose();
return dimg;
}

Related

java image transparency (image lost)

I think something is wrong with paint method, but I can't figure it out.
public void paint(Graphics g) {
screenImage = createImage(1280, 720);
screenGraphic = screenImage.getGraphics();
screenDraw(screenGraphic);
g.drawImage(screenImage, 0, 0, null);
}
public void screenDraw(Graphics g) {
g.drawImage(BG, 0, 0, null);
if(isMainScreen) {
g.drawImage(changedImageAlpha(selectedImage, 120), 130, 360, null);
}
paintComponents(g);
this.repaint();
}
public Image changedImageAlpha(Image image, int trans) {
BufferedImage img = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
Composite c = AlphaComposite.getInstance( AlphaComposite.SRC_ATOP, .5f);
g.setComposite(c);
g.drawImage(image, 0, 0, null);
g.dispose();
int colorMask = 0x00FFFFFF;
int alphaShift = 24;
for(int y=0; y<img.getHeight(); y++){
for(int x=0; x<img.getWidth(); x++) {
img.setRGB(x, y, (img.getRGB(x, y) & colorMask) | (trans << alphaShift));
}
}
return img;
}
BG is an Image object, an also is screenImage.
I expected image to be a transparent image, and yes.
I see some transparent image, but nothing in it. It's just a clear transparent image, no color, nothing.
What could be the problem?
As a comment to the answer to your previous question, I mentioned there was an easier than the bit manipulation suggested in the answer.
But I meant instead of not in addition to!
So this:
public Image changedImageAlpha(Image image, int trans) {
BufferedImage img = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
Composite c = AlphaComposite.getInstance( AlphaComposite.SRC_ATOP, .5f);
g.setComposite(c);
g.drawImage(image, 0, 0, null);
g.dispose();
int colorMask = 0x00FFFFFF;
int alphaShift = 24;
for(int y=0; y<img.getHeight(); y++){
for(int x=0; x<img.getWidth(); x++) {
img.setRGB(x, y, (img.getRGB(x, y) & colorMask) | (trans << alphaShift));
}
}
return img;
}
Should be this:
public Image changedImageAlpha(Image image, int trans) {
BufferedImage img = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
Composite c = AlphaComposite.getInstance( AlphaComposite.SRC_ATOP, .5f);
g.setComposite(c);
g.drawImage(image, 0, 0, null);
g.dispose();
return img;
}
And as a tip: Try to understand the code others are providing. 'Cut & paste' programming usually ends in failure.

Way to change transparency with java swing.image

I draw graphics in frame with this methods.
public void paint(Graphics g) {
screenImage = createImage(1280, 720);
screenGraphic = screenImage.getGraphics();
screenDraw((Graphics2D) screenGraphic);
//g.drawImage(BG, 0, 0, null);
g.drawImage(screenImage, 0, 0, null);
}
public void screenDraw(Graphics2D g) {
g.drawImage(BG, 0, 0, null);
Graphics2D g2 = (Graphics2D)g;
if(isMainScreen) {
//g2.setComposite(alphaComposite);
g2.drawImage(selectedImage, 100, 220, null);
}
paintComponents(g);
this.repaint();
}
I want to have selectedImage to be transparency 50% or other integer.
private Image selectedImage = new ImageIcon(Main.class.getResource("../pic/something.jpg")).getImage();
this is selectedImage, and it works well.
Adapt a bit of Image -> BufferedImage code, and then you can set each pixel to be transparent
Image selectedImage = new ImageIcon([...]).getImage();
//Image -> BufferedImage code
BufferedImage img = new BufferedImage(selectedImage.getWidth(null), selectedImage.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
g.drawImage(selectedImage, 0, 0, null);
g.dispose();
//set each pixel to be transparent
int transparency = 127; //0-255, 0 is invisible, 255 is opaque
int colorMask = 0x00FFFFFF; //AARRGGBB
int alphaShift = 24;
for(int y = 0; y < img.getHeight(); y++)
for(int x = 0; x < img.getWidth(); x++)
img.setRGB(x, y, (img.getRGB(x, y) & colorMask) | (transparency << alphaShift));
//img is now transparent, can replace selectedImage if you want (optional)
selectedImage = img;
If you use ImageIO.read(File) instead of ImageIcon.getImage(), it will give you a BufferedImage directly

Drawing new image over old image java

I need to draw a new image over old image. I first opened both images in BufferedImage and changed their white background to transparent. Then I got a Graphics2D object from the bufferedImage of old image and called drawImage method of Graphics2D class. I then saved the old image to disk. When I open the saved image I find only the old image with white background changed to transparent. Can anyone suggest me what is error with my code or how can I get to fix my error ?
BufferedImage newImage = ImageIO.read(new File("new.png"));
BufferedImage oldImage = ImageIO.read(new File("old.png"));
newImage = makeWhiteTransparent(newImage);
oldImage = makeWhiteTransparent(oldImage);
Graphics2D graphics = (Graphics2D) oldImage.getGraphics();
graphics.drawImage(newImage,null, 0,0);
File outputImage = new File("merged.png");
ImageIO.write(oldImage, "png", outputImage);
My makeWhiteTransparent method goes like this:
public static BufferedImage makeWhiteTransparent(BufferedImage img){
BufferedImage dst = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
dst.getGraphics().drawImage(img, 0, 0, null);
int markerRGB = Color.WHITE.getRGB() | 0xFF000000;
int width = dst.getWidth();
int height = dst.getHeight();
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
int rgb = dst.getRGB(x, y);
if ( ( rgb | 0xFF000000 ) == markerRGB ) {
int value = 0x00FFFFFF & rgb;
dst.setRGB(x, y, value);
}
}
}
return dst;
}
I tried changing graphics.drawImage(newImage, null,0,0) to graphics.drawImage(newImage, 0,0, null) and also changing TYPE_4BYTE_ABGR to TYPE_INT_ARGB as suggested but it did nothing. The error still exists.
This needs to be changed:
graphics.drawImage(newImage,null, 0,0);
to
graphics.drawImage(newImage, 0,0, null);
you are using the wrong version of drawImage - check http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html
--
Change also the type TYPE_4BYTE_ABGR to TYPE_INT_ARGB
--
Here's how it works for me:
public BufferedImage makeWhiteTransparent(BufferedImage img){
BufferedImage dst = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
dst.getGraphics().drawImage(img, 0, 0, null);
int markerRGB = 0x00ffffff; // Color.WHITE.getRGB() | 0xFF000000;
int width = dst.getWidth();
int height = dst.getHeight();
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
int rgb = dst.getRGB(x, y)&0x00ffffff;
if ( rgb == markerRGB ) {
int value = 0x00FFFFFF & rgb;
dst.setRGB(x, y, value);
}
}
}
return dst;
}
bim = makeWhiteTransparent(bim);
bim2 = makeWhiteTransparent(bim2);
Graphics2D graphics = (Graphics2D) bim.getGraphics();
graphics.drawImage(bim2,0,0, null);
g2.drawImage(bim, w/2-wc/2, h/2-hc/2, null);
I got the answer to my question finally. All I had to do was create a new BufferedImage and draw two images over it. Below is the code that works as expected:
BufferedImage newImage = ImageIO.read(new File("new.png"));
BufferedImage oldImage = ImageIO.read(new File("old.png"));
oldImage = makeWhiteTransparent(oldImage);
newImage = makeWhiteTransparent(newImage);
int width = Math.max(newImage.getWidth(), oldImage.getWidth());
int height = Math.max(newImage.getHeight(), oldImage.getHeight());
BufferedImage combined = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics graphics = combined.getGraphics();
graphics.drawImage(oldImage, 0, 0, null);
graphics.drawImage(newImage, 0, 0, null);
File outputImage = new File("merged.png");
ImageIO.write(combined, "PNG", outputImage);

Black Padding with images in java?

I have this problem. I am using this code to rotate an image but the rotated image has black padding in its corners due to rotation.
How could I remove it?
public static BufferedImage rotate(BufferedImage img, int angle) {
rotate_checked = false;
int w = img.getWidth();
int h = img.getHeight();
BufferedImage dimg =new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g = dimg.createGraphics();
g.rotate(Math.toRadians(angle), w/2, h/2);
g.drawImage(img, null, 0, 0);
return dimg;
}
You need to create a transparent image:
BufferedImage buffer = gc.createCompatibleImage(height, width, Transparency.TRANSLUCENT);
where 'gc' is a Graphics2D object. You can also create one directly with new BufferedImage() of course, but this will give you the most efficient-to-use image for your particular graphics context.

BufferedImage from JPanel with transparent background

i'm trying to display an image in my onPaint method, but keep getting a black background. The image i display is the result of a JPanel.print(Graphics) or JPanel.paint(Graphics). So i convert this result to a png file, which is stored in a database. Later this image is loaded and transformed by the user. This process gives me a black background.
Here's the code:
First the image creation:
public static BufferedImage createBufferedImageFromView(JPanel panel){
int w = panel.getWidth();
int h = panel.getHeight();
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
g.setBackground(new Color(0, 0, 0, 0));
g.clearRect(0, 0, bi.getWidth(), bi.getHeight());
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, 0.0f));
panel.setOpaque(false);
panel.repaint();
panel.print(g);
return bi;
}
then i paint it in the onPaint() method:
public void paint(Graphics g) {
super.paint(g);
ParameterBlock params = new ParameterBlock();
params.addSource(previewImage); //source is the input image
int w = previewImage.getWidth(); //Set to the original width of the image
int h = previewImage.getHeight(); //Set to the original height of image
Point tl = new Point((int)cornersTrans[0].getX(), (int)cornersTrans[0].getY()); //The new top left corner
Point tr = new Point((int)cornersTrans[1].getX(), (int)cornersTrans[1].getY()); //The new top right corner
Point bl = new Point((int)cornersTrans[2].getX(), (int)cornersTrans[2].getY()); //The new bottom left corner
Point br = new Point((int)cornersTrans[3].getX(), (int)cornersTrans[3].getY()); //The new bottom right corner
try {
params.add(new WarpPerspective(PerspectiveTransform.getQuadToQuad(0,0, 0, h, w, h, w, 0, tl.x, tl.y, bl.x, bl.y, br.x, br.y, tr.x, tr.y).createInverse()));
} catch (NoninvertibleTransformException e) {
e.printStackTrace();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
if(!isInMovement)
params.add(Interpolation.getInstance(Interpolation.INTERP_BICUBIC));
PlanarImage image = JAI.create("warp", params);
int y0 = (int) (cornersTrans[0].getY() < cornersTrans[1].getY() ? cornersTrans[0].getY() : cornersTrans[1].getY());
int x0 = (int) (cornersTrans[0].getX() < cornersTrans[2].getX() ? cornersTrans[0].getX() : cornersTrans[2].getX());
int x1 = (int) (cornersTrans[3].getX() > cornersTrans[1].getX() ? cornersTrans[3].getX() : cornersTrans[1].getX());
int y1 = (int) (cornersTrans[3].getY() > cornersTrans[2].getY() ? cornersTrans[3].getY() : cornersTrans[2].getY());
Graphics2D g2d = (Graphics2D) g;
// g2d.setBackground(new Color(0, 255, 0, 0) );
// g2d.clearRect(0, 0, 9999, 9999);
g2d.drawImage((Image) image.getAsBufferedImage(), x0,y0,x1, y1, 0, 0, image.getWidth(), image.getHeight(), null);
Any idea of how to fix the black background?
Thanks!

Categories