Reputation: 11
I am very new to Java. I need to add date as bottom-right corner text to captured image by my camera and save it. Like this:
Upvotes: 0
Views: 1176
Reputation: 185
for bottom right corner u can do somthinge like this
private Bitmap addWaterMark(Bitmap src) {
int w = src.getWidth();
int h = src.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
Bitmap waterMark = BitmapFactory.decodeResource(getResources(), R.drawable.watermark3x);
int ww = w-waterMark.getWidth();
int hh = h-waterMark.getHeight();
canvas.drawBitmap(waterMark, ww, hh, null);
return result;
}
Upvotes: 0
Reputation: 1810
Try this code. It's saved lot of effort for me
String d = "18-11-2019";
BufferedImage bi = ImageIO.read(sourceFile);
Graphics2D graphics = bi.createGraphics();
Font font = new Font("ARIAL", Font.PLAIN, 50);
graphics.setFont(font);
graphics.drawString(d, 50, 50);
bi.flush();
ImageIO.write(bi, "jpg", targetFile);
Upvotes: 0
Reputation: 386
Try this
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class WatermarkImage {
public static void main(String[] args) {
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
File origFile = new File("E:/watermark/test.jpg");
ImageIcon icon = new ImageIcon(origFile.getPath());
// create BufferedImage object of same width and height as of original image
BufferedImage bufferedImage = new BufferedImage(icon.getIconWidth(),
icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
// create graphics object and add original image to it
Graphics graphics = bufferedImage.getGraphics();
graphics.drawImage(icon.getImage(), 0, 0, null);
// set font for the watermark text
graphics.setFont(new Font("Arial", Font.BOLD, 20));
String watermark = sdfDate.format(now);
// add the watermark text
graphics.drawString(watermark, (icon.getIconWidth()*80)/100, (icon.getIconHeight()*90)/100);
graphics.dispose();
File newFile = new File("E:/watermark/WatermarkedImage.jpg");
try {
ImageIO.write(bufferedImage, "jpg", newFile);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(newFile.getPath() + " created successfully!");
}
}
Upvotes: 1
Reputation: 25
Try this code.
// get today day. and put into ListView which is situated on bottom-right corner //in xml file.
final Calendar cal = Calendar.getInstance();
year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH)+1;
day = cal.get(Calendar.DAY_OF_MONTH);
TextView out=(TextView)findViewById(R.id.yourtextview);
out.setText(day+"."+month+"."+year);
Upvotes: 0