java图片缩放与裁剪
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 javax.imageio.ImageIO; import com.alibaba.druid.util.StringUtils;
import com.jfinal.kit.StrKit;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder; import java.awt.image.BufferedImage;
import java.rmi.registry.Registry; @SuppressWarnings("restriction")
public class ImageKit {
private final static String[] imgExts=new String[]{"jpg", "jpeg", "png", "bmp"}; public static String getExtName(String fileName){
if(StringUtils.isEmpty(fileName)) return null;
int idx=fileName.lastIndexOf('.');
if(idx!=-1&&(idx+1)<fileName.length()){
return fileName.substring(idx+1);
}else{
return null;
}
}
//通过文件扩展名,是否为支持的图片文件
public static boolean isImageExtName(String fileName){
if(StrKit.isBlank(fileName)){
return false;
}
fileName=fileName.trim().toLowerCase();
String ext=getExtName(fileName);
if(StringUtils.isEmpty(ext)) return false;
for (String str:imgExts){
if(str.equals(ext)){
return true;
}
}
return false;
} public static final boolean notImageExtName(String fileName){
return !isImageExtName(fileName);
}
public static BufferedImage loadImageFils(String sourceImageFileName){
if(notImageExtName(sourceImageFileName)){
throw new IllegalArgumentException("只支持如下几种类型的图像文件:jpg、jpeg、png、bmp");
}
File sourceImageFile=new File(sourceImageFileName);
if(!sourceImageFile.exists()||!sourceImageFile.isFile()){
throw new IllegalArgumentException("文件不存在");
}
try {
return ImageIO.read(sourceImageFile);
}catch (Exception e){
throw new RuntimeException(e);
}
} public static void zoom(int maxWidth,File srcFile,String saveFile){
float quality=0.8f;
try {
BufferedImage srcImage = ImageIO.read(srcFile);
int srcWidth = srcImage.getWidth();
int srcHeight = srcImage.getHeight();
if(srcWidth<=maxWidth){
saveWithQuality(srcImage, quality, saveFile);
}else {
float scalingRatio=(float) maxWidth/(float)srcWidth;
float maxHeight = ((float)srcHeight * scalingRatio);
BufferedImage ret=resize(srcImage,maxWidth,(int) maxHeight);
saveWithQuality(ret, quality, saveFile);
}
}catch (Exception e) {
throw new RuntimeException(e);
}
} public static BufferedImage crop(String sourceImageFile,int left, int top, int width, int height){
if (notImageExtName(sourceImageFile)) {
throw new IllegalArgumentException("只支持如下几种类型的图像文件:jpg、jpeg、png、bmp");
}
try {
BufferedImage bi= ImageIO.read(new File(sourceImageFile));
width = Math.min(width, bi.getWidth());
height = Math.min(height, bi.getHeight());
if(width <= 0) width = bi.getWidth();
if(height <= 0) height = bi.getHeight(); left = Math.min(Math.max(0, left), bi.getWidth() - width);
top = Math.min(Math.max(0, top), bi.getHeight() - height); return bi.getSubimage(left,top,width,height);
}catch (Exception e){
throw new RuntimeException(e);
}
} public static void save(BufferedImage bi,String outputImageFile){
FileOutputStream newImage=null;
try {
ImageIO.write(bi,getExtName(outputImageFile),new File(outputImageFile));
} catch(Exception e){
throw new RuntimeException(e);
} finally {
if(newImage!=null){
try {
newImage.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
} public static BufferedImage resize(BufferedImage bi, int toWidth, int toHeight) {
Graphics g=null;
try {
Image scaledImage = bi.getScaledInstance(toWidth, toHeight, Image.SCALE_SMOOTH);
BufferedImage ret = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);
g = ret.getGraphics();
g.drawImage(scaledImage, 0, 0, null);
return ret;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (g != null) {
g.dispose();
}
}
} public static void saveWithQuality(BufferedImage im, float quality, String outputImageFile){
FileOutputStream newImage = null;
try {
newImage = new FileOutputStream(outputImageFile);
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(newImage);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(im);
jep.setQuality(quality, true);
encoder.encode(im, jep);
}catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (newImage != null) {
try {newImage.close();} catch (IOException e) {throw new RuntimeException(e);}
}
}
} }
java图片缩放与裁剪的更多相关文章
- java图片缩放
package com.rubekid.springmvc.utils; import java.awt.AlphaComposite; import java.awt.Graphics2D; imp ...
- php使用imagick模块实现图片缩放、裁剪、压缩示例
PHP 使用Imagick模块 缩放,裁剪,压缩图片 包括gif图片 缩放 裁剪 复制代码代码如下: /** * 图片裁剪 * 裁剪规则: * 1. 高度为空或为零 按宽度缩放 高度自适 ...
- .NetCore实现图片缩放与裁剪 - 基于ImageSharp
前言 (突然发现断更有段时间了 最近在做博客的时候,需要实现一个类似Lorempixel.LoremPicsum这样的随机图片功能,图片有了,还需要一个根据输入的宽度高度获取图片的功能,由于之前处理图 ...
- java 图片缩放
使用java自带的图片处理api,也可以使用(GraphicsMagick + im4j) import java.awt.Image; import java.awt.image.BufferedI ...
- java多图片上传--前端实现预览--图片压缩 、图片缩放,区域裁剪,水印,旋转,保持比例。
java多图片上传--前端实现预览 前端代码: https://pan.baidu.com/s/1cqKbmjBSXOhFX4HR1XGkyQ 解压后: java后台: <!--文件上传--&g ...
- 用Js+css3实现图片旋转,缩放,裁剪,滤镜
还是前端图片的老话题,花了半天时间,东拼西凑,凑出个demo,优点在于代码少,核心代码就6行,目前刚做了旋转,缩放,裁剪,滤镜要js做,网络上也有现成的代码, 但是想做到自定义的滤镜咋办呢?这还要从底 ...
- java 图片裁剪
图片裁剪功能,我一直以为是前端那边去做,后台不用做过多的考虑,现在我发现,前端去做裁剪好像不是太理想,我在这里简单地介绍一下我们大java的裁剪功能 前端只需要上传,x (x轴),y(y轴) , h( ...
- PHP图片裁剪_图片缩放_PHP生成缩略图
在制作网页过程中,为了排版整齐美观,对网页中的图片处理成固定大小尺寸的图片,或是要截去图片边角中含有水印的图片,对于图片量多,每天更新大量图,靠人工PS处理是不现实的,那么有没有自动处理图片的程序了! ...
- C#图片处理示例(裁剪,缩放,清晰度,水印)
C#图片处理示例(裁剪,缩放,清晰度,水印) 吴剑 2011-02-20 原创文章,转载必需注明出处:http://www.cnblogs.com/wu-jian/ 前言 需求源自项目中的一些应用,比 ...
随机推荐
- map遍历性能记录
map遍历可以通过keySet或者entrySet方式. 性能上:entrySet略胜一筹,原因是keySet获取到key后再根据key去获取value,在查一遍,所以慢一些. keySet: //先 ...
- Shell命令-文件及内容处理之more、less
文件及内容处理 - more.less 1. more:分页显示文件内容 more命令的功能说明 more 命令类似 cat,不过会以一页一页的形式显示,更方便使用者逐页阅读,而最基本的指令就是按空白 ...
- UOJ10 UTR #1 pyx的难题(堆)
显然优先级越高完成的越早,二分答案后用堆模拟就是O(nlog2n)的.考虑去一个log.先固定特殊题的优先级为最低,模拟一遍.这样在特殊题被扔过来到T的这段时间内,如果将特殊题的优先级提高至超过这其中 ...
- react事件处理函数中绑定this的bind()函数
问题引入 import React, { Component } from 'react'; import { Text, View } from 'react-native'; export def ...
- A.02.00—功能定义与唤醒—起始
第一章节主要讲的是模块普通的输入输出,精力及能力有限,仅介绍了一些较为普通的信号,另一些信号留待想了解的人自我探索. 第二章节打算介绍的是功能定义和休眠唤醒相关的内容.也是一些基础内容,对于比较少见或 ...
- A.01.10—模块的输出—PWM高端输出
PWM高端输出比低端输出用得多,如上次提到的卤素灯的控制均是采用高端输出的. PWM高端输出与PWM低端输出的差异就像固态高端输出与固态低端输出的差异类似,从线路失效后对用户的影响来看:高端输出为控制 ...
- idea搭建springboot
1.创建新项目 2.继续项目配置 Name:项目名称Type:我们是Maven构建的,那么选择第一个Maven ProjectPackaging:打包类型,打包成Jar文件Java Version: ...
- Lightning Conductor 洛谷P3515 决策单调性优化DP
遇见的第一道决策单调性优化DP,虽然看了题解,但是新技能√,很开森. 先%FlashHu大佬,反正我是看了他的题解和精美的配图才明白的,%%%巨佬. 废话不多说,看题: 题目大意 已知一个长度为n的序 ...
- openstack项目【day23】:keystone组件HTTP协议
阅读目录 一 为何要学习HTTP协议 二 用户上网过程 三 HTTP协议 part1 http协议概述 part2 请求协议 part3 响应协议 四 抓包分析HTTP协议 一 为何要学习HTTP协议 ...
- 【Java面试题】19 final,finally和finalize的区别
总体区别 final 用于申明属性,方法和类,表示属性不可变,方法不可以被覆盖,类不可以被继承.finally 是异常处理语句结构中,表示总是执行的部分. finallize ...