2016-11-06 23 views
0

我對Java很新。我需要將日期作爲右下角文本添加到我的相機拍攝的圖像中並保存。就像這樣:如何將日期文本(右下角)添加到圖像並保存

enter image description here

+3

嗨ERAY,歡迎SO。我們不是一個編碼編寫服務,像「我想爲X編寫代碼」這樣的問題不會得到回答。請向我們展示您嘗試解決此問題的努力(例如您編寫的代碼),描述您獲得的錯誤/問題,並且我們將盡力幫助您解決這些問題。 –

回答

0

試試這個代碼。

//得到今天的一天。並放入位於右下角的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); 
+0

我可以保存這個嗎?我想將文字添加到圖像並保存爲文件。但是,謝謝你的回答... –

+0

成圖像文件導入圖像位圖打開位圖像帆布和使用代碼上。 – trip03

+0

https://developer.android.com/training/custom-views/custom-drawing.html 我沒有足夠的時間寫它。也許在晚上(如果你願意的話可以寫) – trip03

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

     } 

} 
相關問題