2011-11-12 36 views

回答

7

这是肯定可以的。

要在图像上写入文本,必须将图像加载到Bitmap对象中。然后用Canvas和Paint函数绘制该位图。完成绘图后,只需将位图输出到文件即可。

如果您只是使用黑色背景,最好在画布上创建空白位图,填充黑色,绘制文本然后转储到位图。

I used this tutorial to learn the basics of the canvas and paint.

这就是你要寻找的代码把画布中的图像文件:

OutputStream os = null; 
try { 
    File file = new File(dir, "image" + System.currentTimeMillis() + ".png"); 
    os = new FileOutputStream(file); 
    finalBMP.compress(CompressFormat.PNG, 100, os); 
    finalBMP.recycle(); // this is very important. make sure you always recycle your bitmap when you're done with it. 
    screenGrabFilePath = file.getPath(); 
} catch(IOException e) { 
    finalBMP.recycle(); // this is very important. make sure you always recycle your bitmap when you're done with it. 
    Log.e("combineImages", "problem combining images", e); 
} 
+0

如何在JavaScript中做同样的事情?任何图书馆或任何可以帮助我们做到这一点的东西? – Chetan

6

使用Graphics2d你可以创建一个PNG图像,以及:

public class Imagetest { 

    public static void main(String[] args) throws IOException { 
     File path = new File("image/base/path"); 
     BufferedImage img = new BufferedImage(100, 100, 
       BufferedImage.TYPE_INT_ARGB); 

     Graphics2D g2d = img.createGraphics(); 

     g2d.setColor(Color.YELLOW); 
     g2d.drawLine(0, 0, 50, 50); 

     g2d.setColor(Color.BLACK); 
     g2d.drawLine(50, 50, 0, 100); 

     g2d.setColor(Color.RED); 
     g2d.drawLine(50, 50, 100, 0); 

     g2d.setColor(Color.GREEN); 
     g2d.drawLine(50, 50, 100, 100); 

     ImageIO.write(img, "PNG", new File(path, "1.png")); 
    } 
} 
相关问题