如何将日期文本(右下角)添加到图像并保存
Posted
技术标签:
【中文标题】如何将日期文本(右下角)添加到图像并保存【英文标题】:How can I add date text (Bottom-Right Corner) to image and save 【发布时间】:2017-03-19 19:32:11 【问题描述】:我对 Java 很陌生。我需要将日期作为右下角文本添加到我的相机捕获的图像并保存。像这样:
【问题讨论】:
嗨,Eray,欢迎来到 SO。我们不是编码编写服务,诸如“我想做 X 请为我编写代码”之类的问题将不会得到解答。请向我们展示您为解决此问题所做的努力(例如您编写的代码),描述您遇到的错误/问题,我们将尽力帮助您解决这些问题。 【参考方案1】:试试这个代码。
// 获取今天的日期。并放入位于右下角的ListView //在xml文件中。
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);
【讨论】:
我可以保存这个吗???我想将文本添加到图像并另存为文件。但是感谢您的回答... 到图像文件中导入图像到位图像画布一样打开位图并使用代码上层。 developer.android.com/training/custom-views/custom-drawing.html 没时间写。也许在晚上(如果你会写的话)【参考方案2】:试试这个
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!");
【讨论】:
【参考方案3】:试试这个代码。它为我节省了很多精力
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);
【讨论】:
【参考方案4】:对于右下角你可以做这样的事情
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;
【讨论】:
以上是关于如何将日期文本(右下角)添加到图像并保存的主要内容,如果未能解决你的问题,请参考以下文章