A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王震阳老师 金牌黑马   /  2014-9-20 18:42  /  2870 人查看  /  18 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 就业指导-王震阳老师 于 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. }
复制代码



18 个回复

倒序浏览
基本上可以看懂  但是自己不能弄个写处理啊
回复 使用道具 举报
原理有没有解释一下的?
回复 使用道具 举报
学习额……
回复 使用道具 举报
。。。。。。。。。有点难啊,
回复 使用道具 举报
。。。。为什么没注释?
回复 使用道具 举报
我找找原来做数学建模,有做图像相似拼接的代码。给个很多碎纸片图片,拼接。
回复 使用道具 举报
怎么没注释????
回复 使用道具 举报
貌似很实用,感谢分享
回复 使用道具 举报
老师,com.sun.image.codec.jpeg.JPEGImageEncoder;这个包是不是要单独下载导入进去啊!java自带的没有呢
回复 使用道具 举报
FileOutputStream out = new FileOutputStream(resizedFile);
这句为什么在Eclipse里面报错啊!java.io.FileNotFoundException
回复 使用道具 举报
亚特兰蒂斯 来自手机 高级黑马 2015-3-2 08:12:24
12#
:lol:lol:lol:lol:lol:lol:lol:lol
回复 使用道具 举报
很不错,学习了
回复 使用道具 举报
有用收走。。。。。哈哈
回复 使用道具 举报
邓士林 发表于 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;
要怎么玩啊???
回复 使用道具 举报
运行成功了,压缩效果挺不错
  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. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
王震阳老师 + 1 赞一个!

查看全部评分

回复 使用道具 举报
赶紧收藏
回复 使用道具 举报
666666666666666666666666666666
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马