一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - JAVA教程 - Java實現的圖片高質量縮放類定義與用法示例

Java實現的圖片高質量縮放類定義與用法示例

2021-02-23 10:38CodingSir JAVA教程

這篇文章主要介紹了Java實現的圖片高質量縮放類定義與用法,涉及java針對圖片的運算與轉換等相關操作技巧,需要的朋友可以參考下

本文實例講述了Java實現的圖片高質量縮放類定義與用法。分享給大家供大家參考,具體如下:

找了很多都不理想,最后找個到老外寫的,不得不承認老外寫的確實牛B。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.test;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import javax.swing.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.Kernel;
import java.awt.image.ConvolveOp;
public class ImageUtil {
  public static void resize(File originalFile, File resizedFile,
      int newWidth, float quality) throws IOException {
    if (quality > 1) {
      throw new IllegalArgumentException(
          "Quality has to be between 0 and 1");
    }
    ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
    Image i = ii.getImage();
    Image resizedImage = null;
    int iWidth = i.getWidth(null);
    int iHeight = i.getHeight(null);
    if (iWidth > iHeight) {
      resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight)
          / iWidth, Image.SCALE_SMOOTH);
    } else {
      resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight,
          newWidth, Image.SCALE_SMOOTH);
    }
    // This code ensures that all the pixels in the image are loaded.
    Image temp = new ImageIcon(resizedImage).getImage();
    // Create the buffered image.
    BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),
        temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
    // Copy image to buffered image.
    Graphics g = bufferedImage.createGraphics();
    // Clear background and paint the image.
    g.setColor(Color.white);
    g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
    g.drawImage(temp, 0, 0, null);
    g.dispose();
    // Soften.
    float softenFactor = 0.05f;
    float[] softenArray = { 0, softenFactor, 0, softenFactor,
        1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };
    Kernel kernel = new Kernel(3, 3, softenArray);
    ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
    bufferedImage = cOp.filter(bufferedImage, null);
    // Write the jpeg to a file.
    FileOutputStream out = new FileOutputStream(resizedFile);
    // Encodes image as a JPEG data stream
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    JPEGEncodeParam param = encoder
        .getDefaultJPEGEncodeParam(bufferedImage);
    param.setQuality(quality, true);
    encoder.setJPEGEncodeParam(param);
    encoder.encode(bufferedImage);
  } // Example usage
  public static void main(String[] args) throws IOException {
//    File originalImage = new File("C:\\11.jpg");
//    resize(originalImage, new File("c:\\11-0.jpg"),150, 0.7f);
//    resize(originalImage, new File("c:\\11-1.jpg"),150, 1f);
     File originalImage = new File("C:\\1207.gif");
     resize(originalImage, new File("c:\\1207-0.jpg"),150, 0.7f);
     resize(originalImage, new File("c:\\1207-1.jpg"),150, 1f);
  }
}

希望本文所述對大家java程序設計有所幫助。

原文鏈接:http://blog.csdn.net/educast/article/details/51647066

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 女人pp被扒开流水了 | 欧美日韩亚洲区久久综合 | 日本888 xxxx | 国产久热香蕉在线观看 | 色哟哟在线资源 | 四虎影视4hu最新地址在线884 | 99精品免费视频 | 亚洲 欧美 中文字幕 在线 | 97蝌蚪自拍自窝 | 国产伦码精品一区二区 | 97se亚洲国产综合自在线观看 | 草莓视频旧版 | 国产欧美日韩不卡一区二区三区 | 男人天堂999 | 亚洲国产精品一区二区三区久久 | 国内精品免费一区二区三区 | 亚洲国产婷婷俺也色综合 | 色姑娘久 | 3d欧美人禽交 | 四虎影院精品 | 国产日韩精品一区二区在线观看播放 | 国产喂奶300部 | 久久99精品国产免费观看 | 忘忧草秋观看未满十八 | 青青青久在线视频免费观看 | 美女林柏欣21p人体之仓之梦 | 国产成人亚洲影视在线 | 69热精品视频在线看影院 | 日韩一区视频在线 | 成人18在线观看 | 国产精品成人免费 | 国产夜趣福利第一视频 | 亚洲高清视频在线观看 | 超碰成人在线播放 | 欧美一级一级做性视频 | 日本漫画工囗全彩内番e绅 日本伦理动漫在线观看 | 九九365资源稳定资源站 | 超91在线| 日本深夜影院 | 四色6677最新永久网站 | xxoo做爰猛烈动态 |