激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語言 - JAVA教程 - Java實現(xiàn)的圖片高質(zhì)量縮放類定義與用法示例

Java實現(xiàn)的圖片高質(zhì)量縮放類定義與用法示例

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

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

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

找了很多都不理想,最后找個到老外寫的,不得不承認(rèn)老外寫的確實牛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程序設(shè)計有所幫助。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91精品国产一区二区三区四区在线 | 26uuu成人人网图片 | 亚洲网站一区 | 国产亚洲欧美视频 | 91精品国产91久久久久久丝袜 | 7777奇米成人四色影视 | 神马顶级推理片免费看 | 国产毛片网站 | 欧美精品久久久久久久多人混战 | 国产午夜精品久久久 | 色骚综合 | 91成人在线网站 | 国产一级一区 | 精国产品一区二区三区四季综 | 免费在线观看成年人视频 | 欧美日韩大片在线观看 | 精品一区二区三区免费视频 | 91九色视频在线观看 | 一区二区三区视频播放 | 日韩黄色免费电影 | 福利在线国产 | 久久久国产精品免费观看 | 国产免费传媒av片在线 | 在线观看第一区 | chinesehdxxxx无套| 九九久久视频 | 精品国产一区二区三区天美传媒 | 黄色大片免费看 | 午夜av男人的天堂 | 羞羞网站在线看 | 日韩字幕在线观看 | 日本高清视频网站www | 九一免费在线观看 | 国产成人免费精品 | 久久国产一级 | 亚洲国产精久久久久久久 | 国产精品中文在线 | 欧美视频在线一区二区三区 | xnxx18日本| 人成免费a级毛片 | 欧洲精品久久久 |