各位XDJM好:
我最近遇到了一个问题,就是如何把图片和文字结合起来,生成新的图片,在java中有没有API可以很好的实现这样的效果呢?请大家不吝赐教,谢谢!
------解决方案--------------------
使用java.awt.image.*,javax.imageio.*
和生成验证码的原理差不多
------解决方案--------------------
把字写在图上就行了吧
------解决方案--------------------
没做过,,帮顶
------解决方案--------------------
把字写在图上就行了吧
------解决方案--------------------
看到个验证码的,你可能有用
<%@ page contentType= "image/jpeg "%>
<%@ page import= "java.awt.* "%>
<%@ page import= "java.awt.image.* "%>
<%@ page import= "java.util.* "%>
<%@ page import= "javax.imageio.* "%>
<%
//在内存中创建图象
int iWidth=130,iHeight=18;
BufferedImage image=new BufferedImage(iWidth,iHeight,BufferedImage.TYPE_INT_RGB);
//获取图形上下文
Graphics g=image.getGraphics();
//设定背景色
g.setColor(Color.white);
g.fillRect(0,0,iWidth,iHeight);
//画边框
g.setColor(Color.black);
g.drawRect(0,0,iWidth-1,iHeight-1);
//取随机产生的认证码(4位数字)
String rand=request.getParameter( "Rand ");
rand=rand.substring(0,rand.indexOf( ". "));
switch(rand.length())
{
case 1:rand= "000 "+rand;break;
case 2:rand= "00 "+rand;break;
case 3:rand= "0 "+rand;break;
default:rand=rand.substring(0,4);break;
}
//将认证码存入SESSION
session.setAttribute( "Rand ",rand);
//将认证码显示到图象中
g.setColor(Color.black);
g.setFont(new Font( "Times new Roman ",Font.PLAIN,18));
g.drawString(rand,10,15);
//随机产生88个干扰点,使图象中的认证码不易被其它程序探测到
Random random=new Random();
for(int iIndex=0;iIndex <88;iIndex++)
{
int x=random.nextInt(iWidth);
int y=random.nextInt(iHeight);
g.drawLine(x,y,x,y);
}
//图象生效
g.dispose();
//输出图象到页面
ImageIO.write(image, "JPEG ",response.getOutputStream());
%>
输出时应用验证码
<script> document.write( " <img border=0 src= '/image.jsp?Rand= "+Math.random()*10000+ " '> "); </script> <br>
输认证码: <input name= "Rand " maxlength=4 size= "10 "> <br>
登录认证:
String Rand=request.getParameter( "Rand ");
String strRand=(String)session.getAttribute( "Rand ");
if(!strRand.equals(Rand))
{
//未通过;
}
------解决方案--------------------
package test.csdn;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.text.AttributedString;
import java.awt.font.TextAttribute;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.imageio.ImageIO;
import java.text.AttributedCharacterIterator;
public class TestImg {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
String filePath = "c:\\AnnotationNotes.jpeg ";
ImageIcon imgIcon = new ImageIcon(filePath);
Image aImg = imgIcon.getImage();