* 2.对图片进行适度的大小裁剪 * */ public class ImageUtil { /** * * @param infile 输入文件 * @param outfile 输出文件 * @param srcFormat 源格式 * @param destFormat 输出格式 * @return * @throws Exception */ public static boolean convertFormat(InputStream infile, OutputStream outfile, String srcFormat, String destFormat, int width ,int height) throws Exception { boolean flag = false; BufferedImage src = ImageIO.read(infile); if(height > 0 && width > 0) {// compress the origin image if width and height are non-zero height = src.getHeight() > height height: src.getHeight(); width = src.getWidth() > width width : src.getWidth(); Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);//这个是用来进行图片大小调整的 BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = tag.getGraphics(); //可在下面对图片进行绘制和更改 g.drawImage(image, 0, 0, null); // 绘制缩小后的图 g.dispose(); tag.flush(); flag = ImageIO.write(tag, destFormat, outfile);// 输出到经过缩放的文件流 } else { flag = ImageIO.write(src, destFormat, outfile);//输出原分辨率的图片 } Logger.getLogger(ImageUtil.class).info("图片转换成功: 从[" + srcFormat + "]到[" + destFormat + "]"); return flag; } }
Spring Mvc 上传图片全部过程(二)
// 判断文件夹是否存在,如果不存在则创建文件夹
if (!files.exists()) {
files.mkdir();
}
if (file.getContentType().split("/")[0].equals("image")) {
if (path.endsWith(separator))
fileName = path + "dishpic" + separator + uuid + ".png";
else
fileName = path + separator + "dishpic" + separator + uuid + ".png";
fos = new FileOutputStream(fileName);
ImageUtil.convertFormat(fis, fos, fileType, "png", 0, 0);
fos.flush();
fos.close();
}
} catch (Exception ex) {
System.out.println("文件取出失败,错误信息: " + ex.getMessage());
if (fos != null)
fos.close();
throw ex;
}
return "dishpic" + separator + uuid + ".png";
}
/**
* 1.对图片时行格式转换
* 2.对图片进行适度的大小裁剪 * */ public class ImageUtil { /** * * @param infile 输入文件 * @param outfile 输出文件 * @param srcFormat 源格式 * @param destFormat 输出格式 * @return * @throws Exception */ public static boolean convertFormat(InputStream infile, OutputStream outfile, String srcFormat, String destFormat, int width ,int height) throws Exception { boolean flag = false; BufferedImage src = ImageIO.read(infile); if(height > 0 && width > 0) {// compress the origin image if width and height are non-zero height = src.getHeight() > height height: src.getHeight(); width = src.getWidth() > width width : src.getWidth(); Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);//这个是用来进行图片大小调整的 BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = tag.getGraphics(); //可在下面对图片进行绘制和更改 g.drawImage(image, 0, 0, null); // 绘制缩小后的图 g.dispose(); tag.flush(); flag = ImageIO.write(tag, destFormat, outfile);// 输出到经过缩放的文件流 } else { flag = ImageIO.write(src, destFormat, outfile);//输出原分辨率的图片 } Logger.getLogger(ImageUtil.class).info("图片转换成功: 从[" + srcFormat + "]到[" + destFormat + "]"); return flag; } }
* 2.对图片进行适度的大小裁剪 * */ public class ImageUtil { /** * * @param infile 输入文件 * @param outfile 输出文件 * @param srcFormat 源格式 * @param destFormat 输出格式 * @return * @throws Exception */ public static boolean convertFormat(InputStream infile, OutputStream outfile, String srcFormat, String destFormat, int width ,int height) throws Exception { boolean flag = false; BufferedImage src = ImageIO.read(infile); if(height > 0 && width > 0) {// compress the origin image if width and height are non-zero height = src.getHeight() > height height: src.getHeight(); width = src.getWidth() > width width : src.getWidth(); Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);//这个是用来进行图片大小调整的 BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = tag.getGraphics(); //可在下面对图片进行绘制和更改 g.drawImage(image, 0, 0, null); // 绘制缩小后的图 g.dispose(); tag.flush(); flag = ImageIO.write(tag, destFormat, outfile);// 输出到经过缩放的文件流 } else { flag = ImageIO.write(src, destFormat, outfile);//输出原分辨率的图片 } Logger.getLogger(ImageUtil.class).info("图片转换成功: 从[" + srcFormat + "]到[" + destFormat + "]"); return flag; } }
- <script type="text/java script">BAIDU_CLB_fillSlot("771048");
- 点击复制链接 与好友分享! 回本站首页 <script> function copyToClipBoard(){ var clipBoardContent=document.title + '\r\n' + document.location; clipBoardContent+='\r\n'; window.clipboardData.setData("Text",clipBoardContent); alert("恭喜您!复制成功"); }
<script type="text/java script" id="bdshare_js" data="type=tools&uid=12732">
<script type="text/java script" id="bdshell_js">
<script type="text/java script">
var bds_config = {'snsKey':{'tsina':'2386826374','tqq':'5e544a8fdea646c5a5f3967871346eb8'}};
document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js cdnversion=" + Math.ceil(new Date()/3600000)