Save an Image to mysql - java

click here to open image
This is an image captured from a web cam, and save it to an label. no url used. can i get the image from the label and save it to mysql? NOT USING URL
maybe with getIcon() method?

is how to get image from label
You can create a BufferedImage from any Swing component. The basic code is:
BufferedImage image = new BufferedImage(label.getWidth(), label.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
label.paint( g2d );
g2d.dispose();
Or you can check out Screen Image which adds extra features to the above code to make it more flexible. Then you can just use:
BufferedImage image = ScreenImage.createImage( label );

Related

Java change background color png image

I have been following some StackOverflow links using Graphics2D to change the background color of a BufferedImage.
The project I am working on requires that I read in a png image from a given url; the png image retrieved has a transparent background, and I would like to set it to white.
Here is what I have:
String u = this.format() ;
BufferedImage image = null ;
try{
URL url = new URL(u) ;
image = ImageIO.read(url) ;
Graphics2D graphics = image.createGraphics() ;
graphics.setBackground(Color.WHITE) ;
graphics.clearRect(0, 0, image.getWidth(), image.getHeight()) ;
ImageIO.write(image, "png", new File(outPath + fileName)) ;
graphics.dispose() ;
}
catch(IOException e){
e.printStackTrace() ;
}
The problem I am running into is that when I view the image, the image appears as a solid white box. Apparently I have overlaid a white background on top of the existing image that I retrieved.
How can I preserve the original image and only change the background? Or set the background first and then overlay the retrieved image?
1- Load your image
image = ImageIO.read(url) ;
2- Create a new BufferedImage of the same size
BufferedImage background = new BufferedImage(image.getWidth(), image.getHeight, BufferedImage.TYPE_INT_RGB);
3- Fill the background image with the desired color
Graphics2D g2d = background.createGraphics();
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, background.getWidth(), background.getHeight());
4- Draw the original image onto the background...
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
background is now filled with the desired color and has the image painted on top o it.

Save a BufferedImage to BMP/PNG/JPG and then open it back

I have made a project like Paint from windows and now I want to make the save/open buttons. I have found out how to save the bufferedImage, but the problem is how do I open it back in the correct location and be able to draw on it again?
To read in an image, use ImageIO.
File myPath = new FIle("path to image");
BUfferedImage img = ImageIO.read(myPath);
Also what you could (should) do is load the image into your user space so that you don't edit the original image:
public static BufferedImage userSpace(BufferedImage image)
{
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
Graphics2D graphics = newImage.createGraphics();
graphics.drawRenderedImage(image, null);
graphics.dispose();
return newImage;
}

Removing transparency in PNG BufferedImage

I'm reading a PNG image with the following code:
BufferedImage img = ImageIO.read(new URL(url));
Upon displaying it, there is a black background, which I know is caused from PNG transparency.
I found solutions to this problem suggesting the use of BufferedImage.TYPE_INT_RGB, however I am unsure how to apply this to my code above.
Create a second BufferedImage of type TYPE_INT_RGB...
BufferedImage copy = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
Paint the original to the copy...
Graphics2D g2d = copy.createGraphics();
g2d.setColor(Color.WHITE); // Or what ever fill color you want...
g2d.fillRect(0, 0, copy.getWidth(), copy.getHeight());
g2d.drawImage(img, 0, 0, null);
g2d.dispose();
You now have a non transparent version of the image...
To save the image, take a look at Writing/Saving an Image

How do I ignore the alpha content when loading an Image off disk into a BufferedImage

Currently, when I load an image off disk,
BufferedImage img = ImageIO.read(new File("myfile.png");
The resulting color space is one of ARGB. What I'd prefer is a plain old RGB, but without having to do the filtering myself.
Is there a way to open an image in a certain color mode?
I'm not aware of a way to open it with a specific color format, but you can create a new BufferedImage of the desired format and draw the old image on to it:
BufferedImage img2 = new BufferedImage(img.getWidth(), img.getHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics2D g = img2.createGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
Any transparent parts in the original image will be drawn over a black background in the new image. If you'd prefer a different background, you can insert these lines before the drawImage call:
g.setColor(Color.white); // or whichever
g.fillRect(0, 0, img.getWidth(), img.getHeight());

How can I create an image with Java?

I would like to create a gif image of a filled red circle on a green background. What is the easiest way to do it in Java?
If you already have an image file or image URL, then you can use Toolkit to get an Image:
Image img = Toolkit.getDefaultToolkit().createImage(filename);
If you need to construct a new image raster and paint into the image, then you can use BufferedImage. You can paint onto a BufferedImage, by calling its createGraphics function and painting on the graphics object. To save the BufferedImage into a GIF, you can use the ImageIO class to write out the image.
The best way is to generate a BufferedImage:
BufferedImage img = new BufferedImage(int width, int height, int imageType)
// you can find the Types variables in the api
Then, generated the Graphics2D of this image, this object allows you to set a background and to draw shapes:
Graphics2D g = img.createGraphics();
g.setBackground(Color color) ; //Find how to built this object look at the java api
g.draw(Shape s);
g.dispose(); //don't forget it!!!
To built the image:
File file = new File(dir, name);
try{
ImageIO.write(img, "gif", file);
}catch(IOException e){
e.printStackTrace();
}
Create a BufferedImage and then write it to a file with ImageIO.write(image, "gif", fileName).

Categories

Resources