如何实现把文字和图片合成一张图片比如我现在 要把我这个字和一张图片放到一起组成一张新图,应该怎么实现
如何实现把文字和图片合成一张图片
比如我现在 要把我这个字和一张图片放到一起组成一张新图,应该怎么实现阿
[解决办法]
- 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);
------解决方案--------------------
package com.viewline.tnets.comm;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.AreaAveragingScaleFilter;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 图片处理类
*
* @version 1.0
*/
public class ImageUtil {
/**
* 日志记录
*/
private static final Log LOG = LogFactory.getLog(ImageUtil.class);
/**
* 得到图片宽度
*
* @param imageFile
* @return
*/
public static int getImageWidth(File imageFile) {
try {
Image image = ImageIO.read(imageFile);
return image.getWidth(null);
} catch (IOException e) {
LOG.error("get image " + imageFile.getAbsolutePath()
+ " width error", e);
}
return 0;
}
/**
* 得到图片高度
*
* @param imageFile
* @return
*/
public static int getImageHeight(File imageFile) {
try {
Image image = ImageIO.read(imageFile);
return image.getHeight(null);
} catch (IOException e) {
LOG.error("get image " + imageFile.getAbsolutePath()
+ " height error", e);
}
return 0;
}
/**
* 缩放图片
*
* @param srcImage
* @param destImage
* @param width
* @param height
* @throws IOException
*/
public static void zoomImage(File srcImage, File destImage, int width,
int height) throws IOException {
FileOutputStream out = null;
Graphics graphics = null;
try {
Image src = ImageIO.read(srcImage);
BufferedImage buffer = new BufferedImage(width, height,
BufferedImage.TYPE_3BYTE_BGR);
graphics = buffer.getGraphics();
graphics.drawImage(src, 0, 0, width, height, null);
out = new FileOutputStream(destImage);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(buffer);
} catch (Exception ex) {
LOG.error("zoom image " + srcImage.getAbsolutePath() + " to "
+ destImage.getAbsolutePath() + " width error", ex);
} finally {
if (graphics != null)
graphics.dispose();
if (out != null) {
out.flush();
out.close();
}
}
}
/**
* 图片自动缩放到指定大小
*
* @param srcImage
* @param destImage
* @param width
* @param height
* @throws IOException
*/
public static void autoZoomImage(File srcImage, File destImage, int width,
int height) throws IOException {
try {
int _width = getImageWidth(srcImage);
int _height = getImageHeight(srcImage);
if (width >= _width && height >= _height) {
FileUtil.copyFile(srcImage, destImage);
return;
}
double w = _width * 1.0 / width * 1.0;
double h = _height * 1.0 / height * 1.0;
if (w == h)
zoomImage(srcImage, destImage, width, height);
else if (w > h)
zoomImage(srcImage, destImage, width, (int) (_height / w));
else
zoomImage(srcImage, destImage, (int) (_width / h), height);
} catch (Exception e) {
LOG.error("auto zoom image " + srcImage.getAbsolutePath() + " to "
+ destImage.getAbsolutePath() + " width error", e);
}
}
/**
* 测试
*
* @param args
*/
public static void main(String[] args) {
try {
autoZoomImage(new File("c:/1.jpg"), new File("c:/20000000.jpg"),
160, 120);
System.out.println("over");
} catch (IOException e) {
e.printStackTrace();
}
}
}
