Google提供的Thumbnails对图片的各种操作(缩放、加水印、翻转、转换图片格式)性能比jdk提供的好N倍,压缩出来的图片更清晰

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">jar下载:</span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">http://code.google.com/p/thumbnailator/</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">版本:thumbnailator-0.4.8.jar</span>


 
package com.lvmama.comm.utils.pic;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

@SuppressWarnings("restriction")
public class ImageUtil2 {
	
	/**
     * 缩放图像(按高度和宽度缩放)
     * @param srcImageFile 源图像文件地址
     * @param result 缩放后的图像地址
     * @param height 缩放后的高度
     * @param width 缩放后的宽度
	 * @throws IOException 
     * @isPHP_PC php的pc端调用
     * @isPHP_MOBILE php的移动无线端调用
     */
    public final static void scale(String srcImageFile, String newImageFile, int width,int height, boolean isPHP_PC,boolean isPHP_MOBILE) throws IOException {
        Image img = ImageIO.read(new File(srcImageFile));
        int newWidth = img.getWidth(null),newHeight = img.getHeight(null);
        
        // 计算比例
        if(isPHP_MOBILE && !isPHP_PC && img.getWidth(null) <= width){
        	//无线调用时,宽度没有压缩宽大,则返回原图
        }else if ((img.getHeight(null) > height) || (img.getWidth(null) > width)) {
        	
        	if(isPHP_PC && !isPHP_MOBILE){//PHP的微游记PC端缩略图以定宽或定高(以较长的一边)为基准
                if(img.getWidth(null) > img.getHeight(null)){
                	newWidth = width;
                	newHeight = (int)(new Integer(width).doubleValue()/img.getWidth(null)*img.getHeight(null));
                }else{
                	newHeight = height;
                	newWidth = (int)(new Integer(height).doubleValue()/img.getHeight(null)*img.getWidth(null));
                }
            	
            }else if(isPHP_MOBILE && !isPHP_PC){//PHP的微游记(移动)端缩略图以定宽为基准
            	newWidth = width;
            	newHeight = (int)(new Integer(width).doubleValue()/img.getWidth(null)*img.getHeight(null));
            	
            }else {//vst端:原图长/压缩长 > 原图宽/压缩图宽   ? 原图长/压缩长:原图宽/压缩图宽,以比例较大的为基准压缩
            	// 压缩比判断方法二
                double ratio_height = (new Integer(height)).doubleValue()/ img.getHeight(null);
                double ratio_width = (new Integer(width)).doubleValue() / img.getWidth(null);
                
                if(ratio_height>ratio_width){
                	newHeight = height;
                	newWidth = (int)((new Integer(height)).doubleValue()/ img.getHeight(null) * img.getWidth(null));
                }else{
                	newWidth = width;
                	newHeight = (int)((new Integer(width)).doubleValue() / img.getWidth(null) * img.getHeight(null));
                }
            }
        }
        
    	Thumbnails.of(srcImageFile)
    	.size(newWidth, newHeight)
    	.toFile(newImageFile);
        
    }
	
	/**
	 * 添加图片水印
	 * 
	 * @param targetImg
	 *            目标图片路径,如:C:\\myPictrue\\1.jpg
	 * @param waterImg
	 *            水印图片路径,如:C:\\myPictrue\\logo.png
	 * @param positions 水印图片所在位置
	 * 
	 * @param alpha
	 *            透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明) * @param quality 压缩清晰度
	 *            <b>建议为1.0</b>
	 * @throws IOException 
	 */
	public final static void pressImage(String targetImg, File waterImg, Positions positions, float alpha) throws IOException {
		Thumbnails.of(targetImg)
		.watermark(positions, ImageIO.read(waterImg), alpha)
		.scale(1)//缩放比例
		.toFile(targetImg);
	}

	/**
	 * 把图片印刷到图片上
	 * 
	 * @param pressImg
	 *            -- 水印文件
	 * @param targetImg
	 *            -- 目标文件
	 * @param alpha
	 *            透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明) * @param quality 压缩清晰度
	 *            <b>建议为1.0</b>
	 *            
	 * @throws IOException 
	 */
	public final static void pressImage(String targetImg, String pressImg,float alpha) throws IOException {
		Thumbnails.of(targetImg)
		.watermark(Positions.TOP_LEFT, ImageIO.read(new File(pressImg)), alpha)
		.outputQuality(1)//生成质量100%
		.scale(1)//缩放比例
		.toFile(targetImg);
	}

	/**
	 * 把图片印刷到图片上
	 * 
	 * @param pressImg
	 *            -- 水印文件
	 * @param targetImg
	 *            -- 目标文件
	 * @param position
	 * 
	  @param alpha
	 *            透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明) * @param quality 压缩清晰度
	 *            <b>建议为1.0</b>
	 * 
	 * @throws IOException 
	 */
	public final static void pressImage(String targetImg, String pressImg, Positions position,float alpha) throws IOException {
		Thumbnails.of(targetImg)
		.watermark(position, ImageIO.read(new File(pressImg)), alpha)
		.outputQuality(1)//生成质量100%
		.scale(1)//缩放比例
		.toFile(targetImg);
	}

	/**
	 * 裁剪图片
	 * @throws IOException
	 */
	public static void region(String sourceImg,String newImgPath,int x,int y,int width,int height) throws IOException{
	    //指定坐标  
	    Thumbnails.of(sourceImg)  
	    .sourceRegion(x, y, width, height)//x轴、y轴,裁剪宽、裁剪高
	    .size(width, height)//裁剪后的图片生成的尺寸
	    //设置是否按比例  false,图片可能会走形 默认true,必须在设置尺寸后设置
	    .keepAspectRatio(false) 
	    .toFile(newImgPath);
	}
	
	/**
	 * 旋转图片
	 * @param sourceImage 原图片
	 * @param newImage 生成的新图片
	 * @param degrees 旋转度数
	 * @throws IOException
	 */
	public static void rorate(String sourceImage,String newImage,double degrees) throws IOException{
		Thumbnails.of(sourceImage)
		.rotate(degrees)//旋转度数
		.scale(1)//缩放比例
		.toFile(newImage);
	}
	
	/**
	 * 转换图片格式
	 * @param sourceImg 原图 
	 * @param newImg    转换后的新图
	 * @param format	格式
	 * @throws IOException
	 */
	public static void transferImageFormat(String sourceImg,String newImg,String format) throws IOException{
		Thumbnails.of(sourceImg)
		.outputFormat(format)
		.scale(1)
		.toFile(newImg);
	}

	public static void main(String[] args) {
		try {
			//ImageUtil2.scale("C:\\Users\\chengjiangbo\\Desktop\\images\\c743d228-3ecf-4711-9187-725f252d14b1.jpg", "C:\\Users\\chengjiangbo\\Desktop\\images\\480_300.jpg", 480, 300, true, false);
			//ImageUtil2.pressImage("C:\\Users\\chengjiangbo\\Desktop\\images\\IMG_waterImage.jpg", "C:\\Users\\chengjiangbo\\Desktop\\images\\QRCode.png",Positions.BOTTOM_CENTER, 1);
			//ImageUtil2.region("C:\\Users\\chengjiangbo\\Desktop\\images\\IMG_waterImage.jpg", "C:\\Users\\chengjiangbo\\Desktop\\images\\IMG_waterImage123.jpg",0,0,100,100);
			//ImageUtil2.rorate("C:\\Users\\chengjiangbo\\Desktop\\images\\IMG_waterImage.jpg", "C:\\Users\\chengjiangbo\\Desktop\\images\\IMG_waterImage111.jpg", 90);
			ImageUtil2.transferImageFormat("C:\\Users\\chengjiangbo\\Desktop\\images\\QRCode.png", "C:\\Users\\chengjiangbo\\Desktop\\images\\QRCode.jpg", "JPEG");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}


  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
对于Java中的MultipartFile图片上传而言,可以通过以下步骤实现图片水印的功能: 1. 首先,你需要在前端实现图片的上传功能,将图片以MultipartFile类型的对象传输到后端的controller层。 2. 在controller层接收到图片后,可以使用第三方库,如ImageIO或Thumbnails,来对图片进行处理。这些库提供了丰富的方法来处理图片。 3. 为了给图片水印信息,你可以创建一个水印图片,或者使用文字作为水印。要创建水印图片,你可以使用Java的Graphics2D类,在水印图片上绘制所需的文字或图形。 4. 一旦你有了水印图片,你可以使用Graphics2D的drawImage方法将水印图片绘制到原始图片上。可以通过指定位置和大小来调整水印的位置和大小。 5. 绘制水印后,你可以将带有水印图片保存到指定的目录或返回给前端。 总结一下,要实现Java MultipartFile图片上传水印的功能,你需要在controller层接收到前端上传的图片,使用第三方库对图片进行处理,并在图片上绘制水印。最后,保存带有水印图片或返回给前端。<span class="em">1</span> #### 引用[.reference_title] - *1* [Java MultipartFile实现文件上传并为图片水印(二)](https://blog.csdn.net/weixin_42171478/article/details/113735313)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值