如何向图像添加日期文本(右下角)并保存

时间:2016-11-06 10:57:58

标签: java android date android-camera

我是Java的新手。我需要通过相机将日期作为右下角文本添加到捕获的图像中并保存。像这样:

enter image description here

3 个答案:

答案 0 :(得分:0)

试试这段代码。

//今天到了。并放入位于xml文件中右下角//的ListView。

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);

答案 1 :(得分:0)

试试这个

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!");

      }

}

答案 2 :(得分:0)

尝试此代码。为我省了很多精力

            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);