Add watermark to image - java

I'm trying to add watermark to an image on my website. The way I want it to work is to show the watermark when the image is downloaded or shown on other websites. On my website I want to show it without the watermark. It would be awesome if the watermark is placed on the picture. Is this possible to do the above mentioned by using some picture meta-data or http headers for example? Or is it impossible to perform this and should I do it by adding a footer upon upload and hiding it?

You can reference the follow link:
Add text watermark to image
static void addTextWatermark(String text, File sourceImageFile, File destImageFile) {
try {
BufferedImage sourceImage = ImageIO.read(sourceImageFile);
Graphics2D g2d = (Graphics2D) sourceImage.getGraphics();
// initializes necessary graphic properties
AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f);
g2d.setComposite(alphaChannel);
g2d.setColor(Color.BLUE);
g2d.setFont(new Font("Arial", Font.BOLD, 64));
FontMetrics fontMetrics = g2d.getFontMetrics();
Rectangle2D rect = fontMetrics.getStringBounds(text, g2d);
// calculates the coordinate where the String is painted
int centerX = (sourceImage.getWidth() - (int) rect.getWidth()) / 2;
int centerY = sourceImage.getHeight() / 2;
// paints the textual watermark
g2d.drawString(text, centerX, centerY);
ImageIO.write(sourceImage, "png", destImageFile);
g2d.dispose();
System.out.println("The tex watermark is added to the image.");
} catch (IOException ex) {
System.err.println(ex);
}
}
Add image watermark to image
static void addImageWatermark(File watermarkImageFile, File sourceImageFile, File destImageFile) {
try {
BufferedImage sourceImage = ImageIO.read(sourceImageFile);
BufferedImage watermarkImage = ImageIO.read(watermarkImageFile);
// initializes necessary graphic properties
Graphics2D g2d = (Graphics2D) sourceImage.getGraphics();
AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f);
g2d.setComposite(alphaChannel);
// calculates the coordinate where the image is painted
int topLeftX = (sourceImage.getWidth() - watermarkImage.getWidth()) / 2;
int topLeftY = (sourceImage.getHeight() - watermarkImage.getHeight()) / 2;
// paints the image watermark
g2d.drawImage(watermarkImage, topLeftX, topLeftY, null);
ImageIO.write(sourceImage, "png", destImageFile);
g2d.dispose();
System.out.println("The image watermark is added to the image.");
} catch (IOException ex) {
System.err.println(ex);
}
}
http://www.codejava.net/java-se/graphics/adding-a-watermark-over-an-image-programmatically-using-java

Related

cropping in image into an cricle

I want to crop an image which is rectangular , into a circle of specific diameter. I am able to do it through graphics2D, and I get the image saved, but, when I read it through ImagIO, i get the full image again inspite to it being cropped to a circle. the image is a masked circle, and evrything outside is discarded like a mask. I am attaching the image here. inspite of it being clipped, i get the full image rendered, when i read it through imageIO. here is the code.
int w = bufferedImage.getWidth();
int h = bufferedImage.getHeight();
BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = output.createGraphics();
Area areaOval = new Area(new Arc2D.Double(0, 0, w, w, 0, 360,
Arc2D.PIE));
Shape shapeClipSave = g2.getClip();
g2.setClip(areaOval);
g2.drawImage(bufferedImage, 0, 0, null);
g2.setClip(shapeClipSave);
bufferedImage=output;
try {
ImageIO.write(bufferedImage,"png", new File("D:/new.png"));
bufferedImage= ImageIO.read(new File("D:/new.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g2.dispose();
Here's my take. I rewrote some parts for performance and better fidelity (I couldn't get the edges of the circular area antialiased using clip). Although your code should also work, in general.
public static void main(String[] args) throws IOException {
BufferedImage image = ImageIO.read(new File(args[0]));
// Remove odd borders (imgur issue?)... Remove this if your input doesn't have borders
image = image.getSubimage(10, 0, image.getWidth() - 20, image.getHeight() - 10);
int w = image.getWidth();
int h = image.getHeight();
image = createCircular(image, Math.min(w, h));
if (!ImageIO.write(image, "png", new File("new.png"))) {
System.err.println("Could not write PNG format");
System.exit(1);
}
image = ImageIO.read(new File("new.png"));
showItAll(image);
}
private static BufferedImage createCircular(BufferedImage image, int size) {
BufferedImage output = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = output.createGraphics();
try {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.fillOval(0, 0, size, size);
g2.setComposite(AlphaComposite.SrcIn);
g2.drawImage(image, 0, 0, null);
}
finally {
g2.dispose();
}
return output;
}
private static void showItAll(BufferedImage image) {
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setContentPane(new JPanel() {
{
setBackground(Color.ORANGE);
}
});
frame.getContentPane().add(new JLabel(new ImageIcon(image)));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
Using your giraffe as input, I got the following output, the orange background is just to make the transparent parts clearly visible:
Alternatively, if you use the TwelveMonkeys library and the Adobe Path Support module, you can replace:
image = createCircular(image, Math.min(w, h));
with the following:
int size = Math.min(w, h);
image = Paths.applyClippingPath(new Ellipse2D.Float(0, 0, 1, 1),
image.getSubimage(0, 0, size, size));
Just be aware that the shape coordinates are relative to the size of the image, not in pixels.

Rotate BufferedImage with transparent background

I have an image with transparent background. I'd like to rotate this image to a specific angle and keep the transparent background for the resulting image. For this purpose I use the following method:
public static BufferedImage rotateImage(BufferedImage image, double angle, Color backgroundColor) {
System.out.println(image.getType());
double theta = Math.toRadians(angle);
double sin = Math.abs(Math.sin(theta));
double cos = Math.abs(Math.cos(theta));
int w = image.getWidth();
int h = image.getHeight();
int newW = (int) Math.floor(w * cos + h * sin);
int newH = (int) Math.floor(h * cos + w * sin);
BufferedImage tmp = new BufferedImage(newW, newH, image.getType());
Graphics2D g2d = tmp.createGraphics();
if (backgroundColor != null) {
g2d.setColor(backgroundColor);
g2d.fillRect(0, 0, newW, newH);
}
g2d.fillRect(0, 0, newW, newH);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.translate((newW - w) / 2, (newH - h) / 2);
g2d.rotate(theta, w / 2, h / 2);
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
return tmp;
}
I invoke it with background=null:
BufferedImage image = ImageIO.read(file);
rotateImage(image, 4, null);
ImageIO.write(bi, "PNG", new File("image.png"));
but the background of the resulting image.png is WHITE. What am I doing wrong and how to properly keep the transparent background for image.png?
I'm a bit puzzled about the behavior of Graphics.drawImage(). Maybe somebody else can comment about it.
However, Graphics2D.drawRenderedImage() works a treat. It takes an AffineTransform to control the rotation. The below example nicely works. You probably have additional requirement about the final image size and the location of the rotated image.
import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
public class ImageRotation {
public static void main(String[] args) {
ImageRotation rotation = new ImageRotation();
rotation.rotate("input.png", 45, "output.png");
}
public void rotate(String inputImageFilename, double angle, String outputImageFilename) {
try {
BufferedImage inputImage = ImageIO.read(new File(inputImageFilename));
BufferedImage outputImage = rotateImage(inputImage, angle);
ImageIO.write(outputImage, "PNG", new File(outputImageFilename));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private BufferedImage rotateImage(BufferedImage sourceImage, double angle) {
int width = sourceImage.getWidth();
int height = sourceImage.getHeight();
BufferedImage destImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = destImage.createGraphics();
AffineTransform transform = new AffineTransform();
transform.rotate(angle / 180 * Math.PI, width / 2 , height / 2);
g2d.drawRenderedImage(sourceImage, transform);
g2d.dispose();
return destImage;
}
}
Update
While the above code works for most PNGs, it does not work for the image that alexanoid is using. I've analyzed the image:
It's a grayscale image without a color palette (PNG color type 0) .
It uses simple transparency with a 2 byte long tRNS chunk.
As far as I can tell that's perfectly legal. However, ImageIO does not implement this combination. If the image has no palette, it simply ignores the tRNS chunk and therefore ignores the transparency information. That's most likely a bug.
You basically have two options now:
Look for an alternative library to read PNG files.
Fix the transparency after you have read the PNG file. This only works if know that the image used the particular problematic format.
Input and output for working PNG files
Input image:
Ouptput Image:

Rendering multi-line multi-page formatted text to a BufferedImage (not in Android)

I've just had to implement the creation of a PNG Image, rendering out the contents of a text file. Searching online I found a few implementations using Android but no complete example for multi-line text using standard Java only so thought it would be worth posting my solution here.
The requirements were:
Take a String of potentially any size and render it out with properly formatted paragraphs to fit into PNG images, splitting the String into lines and paragraphs properly. If the rendered document does not fit into one page then generate multiple BufferedImages, one for each page.
I found some sample code in the Java documentation for rendering out a paragraph, building on that I made the following:
private static final Font FONT = new Font("Serif", Font.PLAIN, 14);
private static final float PARAGRAPH_BREAK = 10;
private static final float MARGIN = 20;
private Graphics2D setupGraphics(BufferedImage img) {
Graphics2D g2d = img.createGraphics();
g2d.setFont(FONT);
g2d.fillRect(0, 0, img.getWidth(), img.getHeight());
g2d.setColor(Color.BLACK);
return g2d;
}
private List<BufferedImage> renderText(String str, int width, int height) {
String[] paragraphs = str.split("\n");
List<BufferedImage> images = new ArrayList<>();
BufferedImage img = new BufferedImage(width,
height,
BufferedImage.TYPE_3BYTE_BGR);
images.add(img);
Graphics2D g2d = setupGraphics(img);
float drawPosY = 0;
for (int paragraph=0;paragraph<paragraphs.length;paragraph++) {
drawPosY += PARAGRAPH_BREAK;
AttributedString attStr = new AttributedString(paragraphs[paragraph]);
AttributedCharacterIterator it = attStr.getIterator();
LineBreakMeasurer measurer = new LineBreakMeasurer(it, g2d.getFontRenderContext());
measurer.setPosition(it.getBeginIndex());
while (measurer.getPosition() < it.getEndIndex()) {
TextLayout layout = measurer.nextLayout(img.getWidth()-MARGIN*2);
if (drawPosY > img.getHeight() - layout.getAscent() - layout.getDescent() - layout.getLeading()) {
drawPosY = 0;
img = new BufferedImage((int)(
width,
height,
BufferedImage.TYPE_3BYTE_BGR);
images.add(img);
g2d.dispose();
g2d = setupGraphics(img);
}
drawPosY += layout.getAscent();
layout.draw(g2d, MARGIN, drawPosY);
drawPosY += layout.getDescent()+layout.getLeading();
}
}
g2d.dispose();
return images;
}
In my case I needed the generated PNG in memory so I created it as follows:
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
ImageIO.write(output, "png", baos);
ret.setImageData(baos.toByteArray());
} catch (IOException ex) {
Logger.getLogger(ImageGenerationService.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
Almost identical code in this section would cause ImageIO to write out the file different formats (for example "jpg" instead of "png") or write the image to a file (using FileOutputStream instead of ByteArrayOutputStream).
I hope this helps anyone else with the same problem.

Image Thinning using JAVA

I have written some code in java to convert a colored image into black and white image and then tried to perform thinning on that gray-scale image. Black and white conversion is done successfully, but image thinning is still not giving correct output. Kindly help me in fixing my problem. My code is as follows:
//colored image to black and white conversion; black and white image to thinned image.
public static void main(String[] args)
{
try
{
//colored image path
BufferedImage colored_image = ImageIO.read(new File("D:\\logo.jpg"));
//getting width and height of image
double image_width = colored_image.getWidth();
double image_height = colored_image.getHeight();
BufferedImage img = colored_image;
//drawing a new image
BufferedImage bimg = new BufferedImage((int)image_width, (int)image_height, BufferedImage.TYPE_BYTE_GRAY);
Graphics2D gg = bimg.createGraphics();
gg.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);
//saving black and white image onto drive
String temp = "logo in blackAndwhite.jpeg";
File fi = new File("D:\\" + temp);
ImageIO.write(bimg, "jpg", fi);
//thinning by resizing gray scale image to desired eight and width
BufferedImage bimg2 = new BufferedImage((int)image_width, (int)image_height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bimg2.createGraphics();
// Perform your drawing here
g2.setColor(Color.BLACK);
g2.drawLine(0, 0, 200, 200);
//saving thinned image onto drive
String temp2 = "logo thinned.jpeg";
File fi2 = new File("D:\\" + temp2);
ImageIO.write(bimg2, "jpg", fi2);
//g2.dispose();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Check java.awt.geom.AffineTransform
AffineTransform tx = new AffineTransform();
tx.scale(20, 30);
AffineTransformOp afop = new AffineTransformOp(tx,
AffineTransformOp.TYPE_BILINEAR);
BufferedImage bi = afop.filter(ogininal, null);
Icon icon = new ImageIcon(bi); //here icon will be your thumbnail image

How to resize JLabel ImageIcon?

I'm making a Java Swing application that has the following layout (MigLayout):
[icon][icon][icon][....]
where icon = jlabel and the user can add more icons
When the user adds or removes icons, the others should shrink or grow.
My question is really straightforward: I have a JLabel which contains an ImageIcon; how can I resize this icon?
Try this :
ImageIcon imageIcon = new ImageIcon("./img/imageName.png"); // load the image to a imageIcon
Image image = imageIcon.getImage(); // transform it
Image newimg = image.getScaledInstance(120, 120, java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
imageIcon = new ImageIcon(newimg); // transform it back
(found it here)
Resizing the icon is not straightforward. You need to use Java's graphics 2D to scale the image. The first parameter is a Image class which you can easily get from ImageIcon class. You can use ImageIcon class to load your image file and then simply call getter method to get the image.
private Image getScaledImage(Image srcImg, int w, int h){
BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(srcImg, 0, 0, w, h, null);
g2.dispose();
return resizedImg;
}
And what about it?:
ImageIcon imageIcon = new ImageIcon(new ImageIcon("icon.png").getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT));
label.setIcon(imageIcon);
From: Resize a picture to fit a JLabel
This will keep the right aspect ratio.
public ImageIcon scaleImage(ImageIcon icon, int w, int h)
{
int nw = icon.getIconWidth();
int nh = icon.getIconHeight();
if(icon.getIconWidth() > w)
{
nw = w;
nh = (nw * icon.getIconHeight()) / icon.getIconWidth();
}
if(nh > h)
{
nh = h;
nw = (icon.getIconWidth() * nh) / icon.getIconHeight();
}
return new ImageIcon(icon.getImage().getScaledInstance(nw, nh, Image.SCALE_DEFAULT));
}
One (quick & dirty) way to resize images it to use HTML & specify the new size in the image element. This even works for animated images with transparency.
I agree this code works, to size an ImageIcon from a file for display while keeping the aspect ratio I have used the below.
/*
* source File of image, maxHeight pixels of height available, maxWidth pixels of width available
* #return an ImageIcon for adding to a label
*/
public ImageIcon rescaleImage(File source,int maxHeight, int maxWidth)
{
int newHeight = 0, newWidth = 0; // Variables for the new height and width
int priorHeight = 0, priorWidth = 0;
BufferedImage image = null;
ImageIcon sizeImage;
try {
image = ImageIO.read(source); // get the image
} catch (Exception e) {
e.printStackTrace();
System.out.println("Picture upload attempted & failed");
}
sizeImage = new ImageIcon(image);
if(sizeImage != null)
{
priorHeight = sizeImage.getIconHeight();
priorWidth = sizeImage.getIconWidth();
}
// Calculate the correct new height and width
if((float)priorHeight/(float)priorWidth > (float)maxHeight/(float)maxWidth)
{
newHeight = maxHeight;
newWidth = (int)(((float)priorWidth/(float)priorHeight)*(float)newHeight);
}
else
{
newWidth = maxWidth;
newHeight = (int)(((float)priorHeight/(float)priorWidth)*(float)newWidth);
}
// Resize the image
// 1. Create a new Buffered Image and Graphic2D object
BufferedImage resizedImg = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = resizedImg.createGraphics();
// 2. Use the Graphic object to draw a new image to the image in the buffer
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(image, 0, 0, newWidth, newHeight, null);
g2.dispose();
// 3. Convert the buffered image into an ImageIcon for return
return (new ImageIcon(resizedImg));
}
I found that there is a minor edit to this fix from trolologuy on the last line of code, you will need to implement a new ImageIcon to get the code to compile properly (Yes I know this is 10 years ago). I found this to be an easy fix for a one off issue, but Suken Shah and Mr. Polywhirl have a better fix overall.
ImageIcon imageIcon = new ImageIcon("./img/imageName.png"); // assign image to a new ImageIcon
Image image = imageIcon.getImage(); // transform it
Image newimg = image.getScaledInstance(120, 120, java.awt.Image.SCALE_SMOOTH); // scale it smoothly
ImageIcon newImageIcon = new ImageIcon(newimg); // assign to a new ImageIcon instance

Categories