java图片处理(缩放,放大,logo)

xiaoxiao2026-03-11  10

从别人那整理得来的图片(jpg)处理类

 效果还不错

 

 

 

 

import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class ImageTset { /* * 图片处理 * 放大缩小处理 * logo处理 * */ public static void reduceImg(String imgsrc, int widthdist,int heightdist) { try { File srcfile = new File(imgsrc); if (!srcfile.exists()) { return; } //载入图片文件 Image src = javax.imageio.ImageIO.read(srcfile); int w0 = src.getWidth(null); //得到源图宽 int h0 = src.getHeight(null); //得到源图长 BufferedImage tag= new BufferedImage((int) widthdist, (int) heightdist, BufferedImage.TYPE_INT_RGB); //保存文件 //绘制缩小后的图 tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist, Image.SCALE_SMOOTH), 0, 0, null); //tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist, Image.SCALE_AREA_AVERAGING), 0, 0, null); //标注水印 //int x = widthdist/10*8; //水印位置(x,y) //int y = heightdist/10*8; //jpg_logo( tag , x , y ); //重命名并新建图片 String oleName = imgsrc.substring(imgsrc.indexOf(".")-1, imgsrc.indexOf(".")); String newName = oleName + "v"; String imgdist = imgsrc.replace(oleName, newName); //输出到文件流 FileOutputStream out = new FileOutputStream(imgdist); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); //近JPEG编码 encoder.encode(tag); out.close(); } catch (IOException ex) { ex.printStackTrace(); } } public static void jpg_logo(BufferedImage tag , int x , int y ) { Graphics g = tag.getGraphics(); //g.setColor(Color.BLACK); //以下设置前景色 g.setXORMode(Color.GREEN); g.setFont(new Font("MyFont", Font.ITALIC, 24)); g.drawString("无印", x, y); g.dispose(); } public static void main(String args[]) throws Exception { String f = "f:/1.jpg"; reduceImg(f,168*5,105*5); }

 

相关资源:java 图片缩放处理
转载请注明原文地址: https://www.6miu.com/read-5045726.html

最新回复(0)