黑马程序员技术交流社区

标题: 【阳哥专栏】Java图片缩放 [打印本页]

作者: 王震阳老师    时间: 2014-9-20 18:42
标题: 【阳哥专栏】Java图片缩放
本帖最后由 就业指导-王震阳老师 于 2014-9-20 18:45 编辑

用Java实现图片的缩放。
  1. package com.test;

  2. import com.sun.image.codec.jpeg.JPEGImageEncoder;
  3. import com.sun.image.codec.jpeg.JPEGCodec;
  4. import com.sun.image.codec.jpeg.JPEGEncodeParam;
  5. import javax.swing.*;
  6. import java.io.File;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.awt.*;
  10. import java.awt.image.BufferedImage;
  11. import java.awt.image.Kernel;
  12. import java.awt.image.ConvolveOp;

  13. public class ImageUtil {

  14.         public static void resize(File originalFile, File resizedFile,
  15.                         int newWidth, float quality) throws IOException {

  16.                 if (quality > 1) {
  17.                         throw new IllegalArgumentException(
  18.                                         "Quality has to be between 0 and 1");
  19.                 }

  20.                 ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
  21.                 Image i = ii.getImage();
  22.                 Image resizedImage = null;

  23.                 int iWidth = i.getWidth(null);
  24.                 int iHeight = i.getHeight(null);

  25.                 if (iWidth > iHeight) {
  26.                         resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight)
  27.                                         / iWidth, Image.SCALE_SMOOTH);
  28.                 } else {
  29.                         resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight,
  30.                                         newWidth, Image.SCALE_SMOOTH);
  31.                 }

  32.                 // This code ensures that all the pixels in the image are loaded.
  33.                 Image temp = new ImageIcon(resizedImage).getImage();

  34.                 // Create the buffered image.
  35.                 BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),
  36.                                 temp.getHeight(null), BufferedImage.TYPE_INT_RGB);

  37.                 // Copy image to buffered image.
  38.                 Graphics g = bufferedImage.createGraphics();

  39.                 // Clear background and paint the image.
  40.                 g.setColor(Color.white);
  41.                 g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
  42.                 g.drawImage(temp, 0, 0, null);
  43.                 g.dispose();

  44.                 // Soften.
  45.                 float softenFactor = 0.05f;
  46.                 float[] softenArray = { 0, softenFactor, 0, softenFactor,
  47.                                 1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };
  48.                 Kernel kernel = new Kernel(3, 3, softenArray);
  49.                 ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
  50.                 bufferedImage = cOp.filter(bufferedImage, null);

  51.                 // Write the jpeg to a file.
  52.                 FileOutputStream out = new FileOutputStream(resizedFile);

  53.                 // Encodes image as a JPEG data stream
  54.                 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

  55.                 JPEGEncodeParam param = encoder
  56.                                 .getDefaultJPEGEncodeParam(bufferedImage);

  57.                 param.setQuality(quality, true);

  58.                 encoder.setJPEGEncodeParam(param);
  59.                 encoder.encode(bufferedImage);
  60.         } // Example usage

  61.         public static void main(String[] args) throws IOException {
  62. //                 File originalImage = new File("C:\\11.jpg");
  63. //                 resize(originalImage, new File("c:\\11-0.jpg"),150, 0.7f);
  64. //                 resize(originalImage, new File("c:\\11-1.jpg"),150, 1f);
  65.                  File originalImage = new File("C:\\1207.gif");
  66.                  resize(originalImage, new File("c:\\1207-0.jpg"),150, 0.7f);
  67.                  resize(originalImage, new File("c:\\1207-1.jpg"),150, 1f);
  68.         }
  69. }
复制代码




作者: lishuliang28    时间: 2014-9-20 19:50
基本上可以看懂  但是自己不能弄个写处理啊
作者: lanzy1989    时间: 2014-9-20 21:05
原理有没有解释一下的?
作者: 泡沫__opt    时间: 2014-9-21 10:42
学习额……
作者: @for    时间: 2014-9-23 00:38
。。。。。。。。。有点难啊,
作者: yuZhe_toString    时间: 2014-9-23 20:04
。。。。为什么没注释?

作者: alpha.huai    时间: 2014-10-4 01:04
我找找原来做数学建模,有做图像相似拼接的代码。给个很多碎纸片图片,拼接。
作者: 剛☆_☆    时间: 2014-11-25 22:23
怎么没注释????
作者: 梦开始的地方    时间: 2015-1-17 17:14
貌似很实用,感谢分享
作者: 邓士林    时间: 2015-3-1 11:53
老师,com.sun.image.codec.jpeg.JPEGImageEncoder;这个包是不是要单独下载导入进去啊!java自带的没有呢
作者: 邓士林    时间: 2015-3-1 21:19
FileOutputStream out = new FileOutputStream(resizedFile);
这句为什么在Eclipse里面报错啊!java.io.FileNotFoundException

作者: 亚特兰蒂斯    时间: 2015-3-2 08:12
:lol:lol:lol:lol:lol:lol:lol:lol
作者: shaoshuai    时间: 2015-3-2 11:09
很不错,学习了
作者: yzmaodeng    时间: 2015-3-3 00:15
有用收走。。。。。哈哈
作者: as604049322    时间: 2015-3-15 12:22
邓士林 发表于 2015-3-1 11:53
老师,com.sun.image.codec.jpeg.JPEGImageEncoder;这个包是不是要单独下载导入进去啊!java自带的没有呢 ...

import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
要怎么玩啊???
作者: as604049322    时间: 2015-3-15 12:57
运行成功了,压缩效果挺不错
  1. package teacher.wangzengyang;

  2. import com.sun.image.codec.jpeg.JPEGImageEncoder;
  3. import com.sun.image.codec.jpeg.JPEGCodec;
  4. import com.sun.image.codec.jpeg.JPEGEncodeParam;
  5. import javax.swing.*;
  6. import java.io.File;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.awt.*;
  10. import java.awt.image.BufferedImage;
  11. import java.awt.image.Kernel;
  12. import java.awt.image.ConvolveOp;

  13. public class ImageUtil {

  14.         public static void resize(File originalFile,
  15.                         int newWidth, float quality) throws IOException {

  16.                 if (quality > 1) {
  17.                         throw new IllegalArgumentException(
  18.                                         "Quality has to be between 0 and 1");
  19.                 }
  20.                 String[] name=originalFile.getName().split("\\.");
  21.                 File resizedFile=new File(originalFile.getParentFile(),name[0]+"_new."+name[1]);
  22.                 ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
  23.                 Image i = ii.getImage();
  24.                 Image resizedImage = null;

  25.                 int iWidth = i.getWidth(null);
  26.                 int iHeight = i.getHeight(null);

  27.                 if (iWidth > iHeight) {
  28.                         resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight)
  29.                                         / iWidth, Image.SCALE_SMOOTH);
  30.                 } else {
  31.                         resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight,
  32.                                         newWidth, Image.SCALE_SMOOTH);
  33.                 }

  34.                 // This code ensures that all the pixels in the image are loaded.
  35.                 Image temp = new ImageIcon(resizedImage).getImage();

  36.                 // Create the buffered image.
  37.                 BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),
  38.                                 temp.getHeight(null), BufferedImage.TYPE_INT_RGB);

  39.                 // Copy image to buffered image.
  40.                 Graphics g = bufferedImage.createGraphics();

  41.                 // Clear background and paint the image.
  42.                 g.setColor(Color.white);
  43.                 g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
  44.                 g.drawImage(temp, 0, 0, null);
  45.                 g.dispose();

  46.                 // Soften.
  47.                 float softenFactor = 0.05f;
  48.                 float[] softenArray = { 0, softenFactor, 0, softenFactor,
  49.                                 1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };
  50.                 Kernel kernel = new Kernel(3, 3, softenArray);
  51.                 ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
  52.                 bufferedImage = cOp.filter(bufferedImage, null);

  53.                 // Write the jpeg to a file.
  54.                 FileOutputStream out = new FileOutputStream(resizedFile);

  55.                 // Encodes image as a JPEG data stream
  56.                 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

  57.                 JPEGEncodeParam param = encoder
  58.                                 .getDefaultJPEGEncodeParam(bufferedImage);

  59.                 param.setQuality(quality, true);

  60.                 encoder.setJPEGEncodeParam(param);
  61.                 encoder.encode(bufferedImage);
  62.         } // Example usage

  63.         public static void main(String[] args) throws IOException {
  64. //                 File originalImage = new File("C:\\11.jpg");
  65. //                 resize(originalImage, new File("c:\\11-0.jpg"),150, 0.7f);
  66. //                 resize(originalImage, new File("c:\\11-1.jpg"),150, 1f);
  67.                  File originalImage = new File("E:\\desktop\\新建文件夹\\banner.jpg");
  68.                  resize(originalImage,587, 0.3f);
  69. //                 resize(originalImage,150, 1f);
  70.         }
  71. }
复制代码

作者: 种马先生    时间: 2016-7-6 21:50
赶紧收藏
作者: 15114111253    时间: 2016-8-8 22:08
666666666666666666666666666666




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2