请问 2个大小不同的图片,怎么实现按位置叠加(小的图片按位置覆盖大的图片的某区域),形成一个图片? 如题,就是2个图片按位置叠加。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 同意楼上,drawImage()方法绘制图像和设置绘制位置。如果还需要透明的话,可以用Graphics2D这个类,有设置透明度大小 直接在photo图片上盖logo图片并保存为一个新图片import java.io.*;import java.awt.*;import java.awt.image.*;import javax.swing.*;public class ImagePrint extends JComponent{ public ImagePrint() { init(); } public void init() { try { ImageIcon imageIconlogo = new javax.swing.ImageIcon(Toolkit. getDefaultToolkit().createImage(ImagePrint. class.getResource("/images/printpic.png"))); BufferedImage blogo = new java.awt.image.BufferedImage( imageIconlogo.getIconWidth(), imageIconlogo.getIconHeight(), java.awt.image.BufferedImage.TYPE_INT_ARGB); Graphics g1 = blogo.getGraphics(); g1.drawImage(imageIconlogo.getImage(), 0, 0, this); ImageIcon imageIconphoto = new javax.swing.ImageIcon(Toolkit. getDefaultToolkit().createImage(ImagePrint. class.getResource("/images/bgpic1.jpg"))); BufferedImage bphoto = new java.awt.image.BufferedImage( imageIconphoto.getIconWidth(), imageIconphoto.getIconHeight(), java.awt.image.BufferedImage.TYPE_3BYTE_BGR); Graphics g2 = bphoto.getGraphics(); g2.drawImage(imageIconphoto.getImage(), 0, 0, this); //重合并保存图片 logo右下角和photo右下角的距离2,2 printAndAave(blogo, bphoto, 2, 2); } catch (Exception e) { e.printStackTrace(); } } /** * 进行图片透明印章处理并保存 * @param bprint BufferedImage logo图 * @param bibackground BufferedImage photo图 * @param x int logo右下角和photo右下角x轴方向距离 * @param y int logo右下角和photo右下角y轴方向距离 */ public void printAndAave(BufferedImage blogo, BufferedImage bphoto, int x, int y) { float alpha = 0.5f; // 透明度 Graphics2D g2d = bphoto.createGraphics(); // 设置透明度 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha)); // 开始 // g2d.drawImage(blogo, bphoto.getWidth() - blogo.getWidth() - x, bphoto.getHeight() - blogo.getHeight() - y, null); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); // 结束 FileOutputStream out = null; try { out = new FileOutputStream("image//logonphoto.jpg"); com.sun.image.codec.jpeg.JPEGImageEncoder encoder = com.sun.image. codec.jpeg.JPEGCodec.createJPEGEncoder(out); encoder.encode(bphoto); out.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String args[]) { ImagePrint a = new ImagePrint(); }} 读取图片使用ImageIO类,public static BufferedImage ImageIO.read(File input) ;它返回BufferedImage对象,正符合我们的需求。获取大图片的Graphics对象,如果有需要可以获取Graphics2D对象然后使用Graphics对象的drawImage(Image img, int x, int y, ImageObserver observer) 方法将小图片画到大图片的指定位置。其最后一个参数可以设置为null.然后再使用ImageIO类将图片保存到文件。public static boolean ImageIO.write(RenderedImage im, String formatName, File output) formatName参数可以使用常见的几种图片格式,如JPG,PNG,GIF等。可以使用static String[] ImageIO.getWriterFormatNames() 来获取支持的图片输出格式。同样可以使用static String[] ImageIO.getReaderFormatNames() 来获取支持的图片读取格式。 请问怎么用JAVA取当前时间的给定形式啊? 为什么JAVA的byte型能够表示-256~255呢 真是一个郁闷的超级连接!(我在线等) transient的问题 关于java程序读入数字的问题,如何解决输入非数字字符的问题? C和JAVA 如何读取jar文件中的文件,急!! 请教:给JList加上滚动条后,如何指定滚动条的位置 请问在 JTable 中,如何将焦点总是指向最后一行??? 数据库不能更新 关于java正则的问题 怎样学习用swing做记事本 怎么入门啊
如果还需要透明的话,可以用Graphics2D这个类,有设置透明度大小
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;public class ImagePrint extends JComponent
{
public ImagePrint()
{
init();
} public void init()
{
try
{
ImageIcon imageIconlogo = new javax.swing.ImageIcon(Toolkit.
getDefaultToolkit().createImage(ImagePrint.
class.getResource("/images/printpic.png")));
BufferedImage blogo = new java.awt.image.BufferedImage(
imageIconlogo.getIconWidth(), imageIconlogo.getIconHeight(),
java.awt.image.BufferedImage.TYPE_INT_ARGB);
Graphics g1 = blogo.getGraphics();
g1.drawImage(imageIconlogo.getImage(), 0, 0, this);
ImageIcon imageIconphoto = new javax.swing.ImageIcon(Toolkit.
getDefaultToolkit().createImage(ImagePrint.
class.getResource("/images/bgpic1.jpg")));
BufferedImage bphoto = new java.awt.image.BufferedImage(
imageIconphoto.getIconWidth(),
imageIconphoto.getIconHeight(),
java.awt.image.BufferedImage.TYPE_3BYTE_BGR);
Graphics g2 = bphoto.getGraphics();
g2.drawImage(imageIconphoto.getImage(), 0, 0, this);
//重合并保存图片 logo右下角和photo右下角的距离2,2
printAndAave(blogo, bphoto, 2, 2);
}
catch (Exception e)
{
e.printStackTrace();
}
} /**
* 进行图片透明印章处理并保存
* @param bprint BufferedImage logo图
* @param bibackground BufferedImage photo图
* @param x int logo右下角和photo右下角x轴方向距离
* @param y int logo右下角和photo右下角y轴方向距离
*/
public void printAndAave(BufferedImage blogo, BufferedImage bphoto,
int x, int y)
{
float alpha = 0.5f; // 透明度
Graphics2D g2d = bphoto.createGraphics(); // 设置透明度
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha)); // 开始 //
g2d.drawImage(blogo, bphoto.getWidth() - blogo.getWidth() - x,
bphoto.getHeight() - blogo.getHeight() - y, null);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); // 结束
FileOutputStream out = null;
try
{
out = new FileOutputStream("image//logonphoto.jpg");
com.sun.image.codec.jpeg.JPEGImageEncoder encoder = com.sun.image.
codec.jpeg.JPEGCodec.createJPEGEncoder(out);
encoder.encode(bphoto);
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
} public static void main(String args[])
{
ImagePrint a = new ImagePrint();
}
}
它返回BufferedImage对象,正符合我们的需求。
获取大图片的Graphics对象,如果有需要可以获取Graphics2D对象
然后使用Graphics对象的drawImage(Image img, int x, int y, ImageObserver observer)
方法将小图片画到大图片的指定位置。其最后一个参数可以设置为null.
然后再使用ImageIO类将图片保存到文件。
public static boolean ImageIO.write(RenderedImage im, String formatName, File output)
formatName参数可以使用常见的几种图片格式,如JPG,PNG,GIF等。
可以使用static String[] ImageIO.getWriterFormatNames() 来获取支持的图片输出格式。
同样可以使用static String[] ImageIO.getReaderFormatNames() 来获取支持的图片读取格式。