BufferedImage Size overridden

I created a Buffered image "bufi" with a specific dimension. When I read in a gif file image into bufi the size of bufi is altered. How can I make the image I read in, assume the size of bufi.Here is the code I used.

File in = new File("C:\\ ImageDir\\holderImage.gif");

int width = 50;

int height = 10;

BufferedImage bufi = new BufferedImage(width, height,

BufferedImage.TYPE_BYTE_BINARY);

bufi = ImageIO.read(in);

Thanks for your help.

[493 byte] By [kxeta] at [2007-11-27 6:11:57]
# 1

> When I read in a gif file image into bufi the size of bufi is altered.

What happens is that ImageIO.read() creates another BufferedImage and the one that your bufi variable used to reference goes to wherever good objects go when nothing references them any more.

> How can I make the image I read in, assume the size of bufi.

One way would be to create bufi as you do at the moment using read(). Then create another BufferedImage the size you want and draw the first image into the graphics context of the second.

[Edit] Have a look at this thread: http://forum.java.sun.com/thread.jspa?threadID=522483 where DrLazloJamf answers Hippolyte's question regarding the basics of image reading/writing/displaying/scaling.

pbrockway2a at 2007-7-12 17:18:41 > top of Java-index,Java Essentials,Java Programming...
# 2
Scale the image AFTER the load(read).
hiwaa at 2007-7-12 17:18:41 > top of Java-index,Java Essentials,Java Programming...
# 3

It's because ImageIO.read() returns a new BufferedImage. You could do something like this.

File in = new File("C:\\ ImageDir\\holderImage.gif");

int width = 50;

int height = 10;

BufferedImage bufi = new BufferedImage(width, height,

BufferedImage.TYPE_BYTE_BINARY);

BufferedImage tempImage = ImageIO.read(in);

Graphics g = bufi.getGraphics(); //get Graphics object from original image

g.drawImage(tempImage, 0, 0, null); //paint image read from file onto original image

g.dispose();

CaptainMorgan08a at 2007-7-12 17:18:41 > top of Java-index,Java Essentials,Java Programming...
# 4
> [Edit] Have a look at this thread:...> where DrLazloJamf answers Hippolyte's question...But... DrLaszloJamf is Hippolyte. How does that work?
CaptainMorgan08a at 2007-7-12 17:18:41 > top of Java-index,Java Essentials,Java Programming...
# 5
Yes. I knew the thread existed, and was *the* answer to this problem, but the significance of the names only becomes apparent years later. Should of guessed though - I mean reply 1 must have taken longer than 5 minutes to compose!
pbrockway2a at 2007-7-12 17:18:41 > top of Java-index,Java Essentials,Java Programming...
# 6
> > [Edit] Have a look at this thread:...> > where DrLazloJamf answers Hippolyte's question...> > But... DrLaszloJamf is Hippolyte. How> does that work?And people accuse us (mainly me) of duke shenanigans. :(
cotton.ma at 2007-7-12 17:18:41 > top of Java-index,Java Essentials,Java Programming...
# 7
> And people accuse us (mainly me) of duke shenanigans.Don't worry. Several people got pissed at me when I made a thread that made you post a new thread in the Monitor and Maintain forum when you tried to click on it.
CaptainMorgan08a at 2007-7-12 17:18:41 > top of Java-index,Java Essentials,Java Programming...
# 8

> > And people accuse us (mainly me) of duke

> shenanigans.

>

> Don't worry. Several people got pissed at me when I

> made a thread that made you post a new thread in the

> Monitor and Maintain forum when you tried to click on

> it.

As well they should. :p

Just kidding. I was joking too about the duke shenanigans. There was some upset this morning. ;) All better now.

cotton.ma at 2007-7-12 17:18:41 > top of Java-index,Java Essentials,Java Programming...
# 9

> > > [Edit] Have a look at this thread:...

> > > where DrLazloJamf answers Hippolyte's

> question...

> >

> > But... DrLaszloJamf is Hippolyte.

> How

> does that work?

>

> And people accuse us (mainly me) of duke shenanigans.

>

>

> :(

the bank caught me doing duke shenanigans once and i got into duke debt.

prob.not.sola at 2007-7-12 17:18:41 > top of Java-index,Java Essentials,Java Programming...
# 10
Alright, I'm going to pistol-whip the next person who says shenanigans!
CaptainMorgan08a at 2007-7-12 17:18:41 > top of Java-index,Java Essentials,Java Programming...
# 11
> Alright, I'm going to pistol-whip the next person who> says shenanigans!whatever
prob.not.sola at 2007-7-12 17:18:41 > top of Java-index,Java Essentials,Java Programming...
# 12
> whateverYou've never seen Supertroopers, have you?
CaptainMorgan08a at 2007-7-12 17:18:41 > top of Java-index,Java Essentials,Java Programming...
# 13
> > whatever> > You've never seen Supertroopers, have you?no.
prob.not.sola at 2007-7-12 17:18:41 > top of Java-index,Java Essentials,Java Programming...
# 14
nevermind me; i am incredibly humourless at the moment.
prob.not.sola at 2007-7-12 17:18:41 > top of Java-index,Java Essentials,Java Programming...
# 15

Have a look at this thread: http://forum.java.sun.com/thread.jspa?threadID=522483 where DrLazloJamf answers Hippolyte's question regarding the basics of image reading/writing/displaying/scaling.

Yes DrLazioJamf aswers are excellent. It helped me solved the problem of resizing my image.Thanks a lot.

There is a second problem - totally different from the resizing.The image is a cursive writing like a person's signature. It has a white background and I need to render it to other backgrounds. After rendering it to a background most ot the writing is blurred out or do not appear at all.I'm using getRGB() and setRGB(). This is he code I used - "resize" is the Buffered Image. Thanks

Color black = new Color(0, 0, 0);

int blackRGB = black.getRGB();

Color bkgColor = new Color(255,196,255);

int bkgRGB = bkgColor.getRGB();

for (int i = 0; i < resize.getWidth(); i++) {

for (int j = 0; j < resize.getHeight(); j++) {

int pixColor = resize.getRGB(i,j);

if (pixColor != blackRGB){

resize.setRGB(i,j, bkgRGB);

}

}

}

kxeta at 2007-7-21 21:45:31 > top of Java-index,Java Essentials,Java Programming...
# 16

Your for loops will take each non black pixel of resize and make it a light background colour. You may well have a problem because the pixels that represent writing might not be exactly black. They might be a very very dark gray.

You could check the red, green and blue components of the pixel and, if they are all less than 10 say then use the background colour. Or there might be a better way that someone can suggest (using Composite?).

pbrockway2a at 2007-7-21 21:45:31 > top of Java-index,Java Essentials,Java Programming...
# 17

> Your for loops will take each non black pixel of

> resize and make it a light background colour. You

> may well have a problem because the pixels that

> represent writing might not be exactly black.

> They might be a very very dark gray.

>

I don't know much about dealing with this programmatically but I do know something about how I would handle this in Photoshop which sounds to me applicable.

As you say the problem is because the cursive writing is not in fact black pixels (or very few anyway) but is mostly made up of grays. In this case I would seek to remove /change all the white pixels instead because I know those are not writing for sure.

Is that possible?

cotton.ma at 2007-7-21 21:45:31 > top of Java-index,Java Essentials,Java Programming...
# 18

>

> As you say the problem is because the cursive writing

> is not in fact black pixels (or very few anyway) but

> is mostly made up of grays. In this case I would seek

> to remove /change all the white pixels instead

> because I know those are not writing for sure.

>

> Is that possible?

Yes when I encounter this problem I tried that method of changing the whiite pixels to background color, that too has a problem.The resulting writing appears to be highlighted in white.

kxeta at 2007-7-21 21:45:31 > top of Java-index,Java Essentials,Java Programming...
# 19

> >

> > As you say the problem is because the cursive

> writing

> > is not in fact black pixels (or very few anyway)

> but

> > is mostly made up of grays. In this case I would

> seek

> > to remove /change all the white pixels instead

> > because I know those are not writing for sure.

> >

> > Is that possible?

>

> Yes when I encounter this problem I tried that

> method of changing the whiite pixels to background

> color, that too has a problem.The resulting

> writing appears to be highlighted in white.

Hmmm. I understand. Sorry no idea. Seems tricky. I'd be interested in knowing how you resolve this when you do though.

cotton.ma at 2007-7-21 21:45:31 > top of Java-index,Java Essentials,Java Programming...
# 20

Actually it's quite simple; you just have to map some function of the colours to the alpha level. The first thing that comes to mind is a direct linear map:import java.awt.image.*;

import javax.imageio.*;

import javax.swing.*;

import java.awt.Image;

class Alpha

{

static RGBImageFilter alphaFilter = new RGBImageFilter()

{

{

canFilterIndexColorModel = true;

}

public int filterRGB(int x, int y, int rgb)

{

int alphaLevel = getAlphaLevel(rgb);

return (alphaLevel << 24) | (rgb & 0xFFFFFF);

}

private int getAlphaLevel(int rgb)

{

return 255-((rgb & 0xFF) + ((rgb & 0xFF00) >> 8)

+ ((rgb & 0xFF0000) >> 16))/3;

}

};

public static void main(String[] argh) throws Exception

{

Image src = ImageIO.read(

new java.net.URL("http://blog.sun.com/prats/resource/ThumbsUp.jp

Image dst = java.awt.Toolkit.getDefaultToolkit().createImage(

new FilteredImageSource(src.getSource(), alphaFilter));

JOptionPane.showMessageDialog(null, new ImageIcon(dst));

}

}

Unfortunately you need to muck about a little to get a buffered image, the awt toolkit doesn't create them.

jsalonena at 2007-7-21 21:45:31 > top of Java-index,Java Essentials,Java Programming...