日期:2014-05-20  浏览次数:20727 次

如何实现把文字和图片合成一张图片
比如我现在 要把我这个字和一张图片放到一起组成一张新图,应该怎么实现阿

------解决方案--------------------
Java code
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 
import java.awt.image.*; 
import org.w3c.dom.*; 
import com.sun.image.codec.jpeg.*; 
import javax.imageio.*; 

public class ImgBean{ 
public void ImgBean(){} 
public void ImgYin(String s,String ImgName){ 
try{ 
File _file = new File(ImgName); 
Image src = ImageIO.read(_file); 
int wideth=src.getWidth(null); 
int height=src.getHeight(null); 
BufferedImage image=new BufferedImage(wideth,height,BufferedImage.TYPE_INT_RGB); 
Graphics g=image.createGraphics(); 
g.drawImage(src,0,0,wideth,height,null); 
String s="我要加的水印"; 
g.setColor(Color.RED); 
g.setFont(new Font("宋体",Font.PLAIN,20)); 
Font aa=new Font("宋体",Font.PLAIN,20); 

g.drawString(s,wideth-150,height-10); 
g.dispose(); 
FileOutputStream out=new FileOutputStream(ImgName); 
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
encoder.encode(image); 
out.close(); 
} 
catch(Exception e){ 
System.out.println(e); 
} 
} 
}

------解决方案--------------------
ImageIO就行,就是自己拼图,先来一张画布,计算下图片和文字的像素长宽,然后有用drawString和drawImage,最后可以借助第三方的包,生成各种格式的图片
------解决方案--------------------
在图片库里提供此接口
------解决方案--------------------
我改了下,呵呵,字体颜色再改改,换张图,调调字的位置就可以了
Java code

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package test1;

/**
 *
 * @author ilrxx
 */
import java.awt.*; 
import java.io.*; 
import java.awt.image.*; 
import com.sun.image.codec.jpeg.*; 
import javax.imageio.*; 

public class ImgBean{ 
public void ImgBean(){} 
public static void ImgYin(String s,String ImgName){ 
    byte[] bytes = null; 
    try{ 
        String str = s;
        File _file = new File(ImgName); 
        Image src = ImageIO.read(_file); 
        int wideth=src.getWidth(null); 
        int height=src.getHeight(null); 
        BufferedImage image=new BufferedImage(wideth,height,BufferedImage.TYPE_INT_RGB); 
        Graphics g=image.createGraphics(); 
        g.drawImage(src,0,0,wideth,height,null); 
        g.setColor(Color.RED); 
        g.setFont(new Font("宋体",Font.PLAIN,20)); 
        Font aa=new Font("宋体",Font.PLAIN,20); 

        g.drawString(str,wideth-150,height-10); 
        g.dispose(); 
        ByteArrayOutputStream out1 = new ByteArrayOutputStream();
        saveImage(image, out1);
        bytes = out1.toByteArray();
        out1.close();
        FileOutputStream out2 = new FileOutputStream(ImgName);
        out2.write(bytes);
        out2.close();
    } 
    catch(Exception e){ 
        System.out.println(e); 
    } 
} 
public static void saveImage(BufferedImage img, OutputStream out1) throws Exception {
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out1);
    encoder.encode(img);
}

public static void main(String[] args){
    ImgYin("我要加的水印" , "d:/nbactivities.jpg");
}
}

------解决方案--------------------
如果要输出就在用字节流读生成到本地的那个图片就可以了
------解决方案--------------------
使用制图软件就可以了
------解决方案--------------------
看你的平台了,最终极的是使用字库,然后把字库点阵画到图片上,如果有特效就是图像数据的处理,这样不依赖于任何平台,在LINUX下WINDOWS下都可用。
------解决方案--------------------
BufferImage image = new BufferImage(...);
Graphics g = BufferImage.getGraphics();
g.drawImage(...);
g.drawString(...);
ImageIO.write(image, "JPEG", outputStream);
------解决方案--------------------