欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java生成縮略圖的方法示例

 更新時間:2017年03月25日 12:14:14   作者:ITshu  
這篇文章主要介紹了java生成縮略圖的方法,結(jié)合具體實例形式分析了java生成縮略圖過程中所涉及的各種常見的圖形處理技巧,需要的朋友可以參考下

本文實例講述了java生成縮略圖的方法。分享給大家供大家參考,具體如下:

package com.util;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* 生成壓縮圖
*
*/
public class ImageScale {
private int width;
private int height;
private int scaleWidth;
double support = (double) 3.0;
double PI = (double) 3.14159265358978;
double[] contrib;
double[] normContrib;
double[] tmpContrib;
int startContrib, stopContrib;
int nDots;
int nHalfDots;
/**
* Start: Use Lanczos filter to replace the original algorithm for image
* scaling. Lanczos improves quality of the scaled image modify by :blade
*/
public BufferedImage imageZoomOut(BufferedImage srcBufferImage, int w, int h) {
width = srcBufferImage.getWidth();
height = srcBufferImage.getHeight();
scaleWidth = w;
if (DetermineResultSize(w, h) == 1) {
return srcBufferImage;
}
CalContrib();
BufferedImage pbOut = HorizontalFiltering(srcBufferImage, w);
BufferedImage pbFinalOut = VerticalFiltering(pbOut, h);
return pbFinalOut;
}
/**
* 決定圖像尺寸
*/
private int DetermineResultSize(int w, int h) {
double scaleH, scaleV;
// update by libra
double wt = w > width ? width : w;
double ht = h > height ? height : h;
scaleH = (double) wt / (double) width;
scaleV = (double) ht / (double) height;
// 需要判斷一下scaleH,scaleV,不做放大操作
if (scaleH >= 1.0 && scaleV >= 1.0) {
return 1;
}
return 0;
} // end of DetermineResultSize()
private double Lanczos(int i, int inWidth, int outWidth, double Support) {
double x;
x = (double) i * (double) outWidth / (double) inWidth;
return Math.sin(x * PI) / (x * PI) * Math.sin(x * PI / Support)
/ (x * PI / Support);
} // end of Lanczos()
//
// Assumption: same horizontal and vertical scaling factor
//
private void CalContrib() {
nHalfDots = (int) ((double) width * support / (double) scaleWidth);
nDots = nHalfDots * 2 + 1;
try {
contrib = new double[nDots];
normContrib = new double[nDots];
tmpContrib = new double[nDots];
} catch (Exception e) {
System.out.println("init contrib,normContrib,tmpContrib" + e);
}
int center = nHalfDots;
contrib[center] = 1.0;
double weight = 0.0;
int i = 0;
for (i = 1; i <= center; i++) {
contrib[center + i] = Lanczos(i, width, scaleWidth, support);
weight += contrib[center + i];
}
for (i = center - 1; i >= 0; i--) {
contrib[i] = contrib[center * 2 - i];
}
weight = weight * 2 + 1.0;
for (i = 0; i <= center; i++) {
normContrib[i] = contrib[i] / weight;
}
for (i = center + 1; i < nDots; i++) {
normContrib[i] = normContrib[center * 2 - i];
}
} // end of CalContrib()
// 處理邊緣
private void CalTempContrib(int start, int stop) {
double weight = 0;
int i = 0;
for (i = start; i <= stop; i++) {
weight += contrib[i];
}
for (i = start; i <= stop; i++) {
tmpContrib[i] = contrib[i] / weight;
}
} // end of CalTempContrib()
private int GetRedValue(int rgbValue) {
int temp = rgbValue & 0x00ff0000;
return temp >> 16;
}
private int GetGreenValue(int rgbValue) {
int temp = rgbValue & 0x0000ff00;
return temp >> 8;
}
private int GetBlueValue(int rgbValue) {
return rgbValue & 0x000000ff;
}
private int ComRGB(int redValue, int greenValue, int blueValue) {
return (redValue << 16) + (greenValue << 8) + blueValue;
}
// 行水平濾波
private int HorizontalFilter(BufferedImage bufImg, int startX, int stopX,
int start, int stop, int y, double[] pContrib) {
double valueRed = 0.0;
double valueGreen = 0.0;
double valueBlue = 0.0;
int valueRGB = 0;
int i, j;
for (i = startX, j = start; i <= stopX; i++, j++) {
valueRGB = bufImg.getRGB(i, y);
valueRed += GetRedValue(valueRGB) * pContrib[j];
valueGreen += GetGreenValue(valueRGB) * pContrib[j];
valueBlue += GetBlueValue(valueRGB) * pContrib[j];
}
valueRGB = ComRGB(Clip((int) valueRed), Clip((int) valueGreen),
Clip((int) valueBlue));
return valueRGB;
} // end of HorizontalFilter()
// 圖片水平濾波
private BufferedImage HorizontalFiltering(BufferedImage bufImage, int iOutW) {
int dwInW = bufImage.getWidth();
int dwInH = bufImage.getHeight();
int value = 0;
BufferedImage pbOut = new BufferedImage(iOutW, dwInH,
BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < iOutW; x++) {
int startX;
int start;
int X = (int) (((double) x) * ((double) dwInW) / ((double) iOutW) + 0.5);
int y = 0;
startX = X - nHalfDots;
if (startX < 0) {
startX = 0;
start = nHalfDots - X;
} else {
start = 0;
}
int stop;
int stopX = X + nHalfDots;
if (stopX > (dwInW - 1)) {
stopX = dwInW - 1;
stop = nHalfDots + (dwInW - 1 - X);
} else {
stop = nHalfDots * 2;
}
if (start > 0 || stop < nDots - 1) {
CalTempContrib(start, stop);
for (y = 0; y < dwInH; y++) {
value = HorizontalFilter(bufImage, startX, stopX, start,
stop, y, tmpContrib);
pbOut.setRGB(x, y, value);
}
} else {
for (y = 0; y < dwInH; y++) {
value = HorizontalFilter(bufImage, startX, stopX, start,
stop, y, normContrib);
pbOut.setRGB(x, y, value);
}
}
}
return pbOut;
} // end of HorizontalFiltering()
private int VerticalFilter(BufferedImage pbInImage, int startY, int stopY,
int start, int stop, int x, double[] pContrib) {
double valueRed = 0.0;
double valueGreen = 0.0;
double valueBlue = 0.0;
int valueRGB = 0;
int i, j;
for (i = startY, j = start; i <= stopY; i++, j++) {
valueRGB = pbInImage.getRGB(x, i);
valueRed += GetRedValue(valueRGB) * pContrib[j];
valueGreen += GetGreenValue(valueRGB) * pContrib[j];
valueBlue += GetBlueValue(valueRGB) * pContrib[j];
// System.out.println(valueRed+"->"+Clip((int)valueRed)+"<-");
//
// System.out.println(valueGreen+"->"+Clip((int)valueGreen)+"<-");
// System.out.println(valueBlue+"->"+Clip((int)valueBlue)+"<-"+"-->");
}
valueRGB = ComRGB(Clip((int) valueRed), Clip((int) valueGreen),
Clip((int) valueBlue));
// System.out.println(valueRGB);
return valueRGB;
} // end of VerticalFilter()
private BufferedImage VerticalFiltering(BufferedImage pbImage, int iOutH) {
int iW = pbImage.getWidth();
int iH = pbImage.getHeight();
int value = 0;
BufferedImage pbOut = new BufferedImage(iW, iOutH,
BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < iOutH; y++) {
int startY;
int start;
int Y = (int) (((double) y) * ((double) iH) / ((double) iOutH) + 0.5);
startY = Y - nHalfDots;
if (startY < 0) {
startY = 0;
start = nHalfDots - Y;
} else {
start = 0;
}
int stop;
int stopY = Y + nHalfDots;
if (stopY > (int) (iH - 1)) {
stopY = iH - 1;
stop = nHalfDots + (iH - 1 - Y);
} else {
stop = nHalfDots * 2;
}
if (start > 0 || stop < nDots - 1) {
CalTempContrib(start, stop);
for (int x = 0; x < iW; x++) {
value = VerticalFilter(pbImage, startY, stopY, start, stop,
x, tmpContrib);
pbOut.setRGB(x, y, value);
}
} else {
for (int x = 0; x < iW; x++) {
value = VerticalFilter(pbImage, startY, stopY, start, stop,
x, normContrib);
pbOut.setRGB(x, y, value);
}
}
}
return pbOut;
} // end of VerticalFiltering()
int Clip(int x) {
if (x < 0)
return 0;
if (x > 255)
return 255;
return x;
}
/**
* End: Use Lanczos filter to replace the original algorithm for image
* scaling. Lanczos improves quality of the scaled image modify by :blade
*/
public boolean scale(String source, String target, int width, int height) {
File f = new File(source);
try {
BufferedImage bi = ImageIO.read(f);
BufferedImage out = null;
ImageScale scal = new ImageScale();
int _width = bi.getWidth();// add
int _height = bi.getHeight();// add
int[] _arr = this.getImageWidthAndHeight(_width, _height, width,
height);// add
// out = scal.imageZoomOut(bi, width, height);
out = scal.imageZoomOut(bi, _arr[0], _arr[1]);
File t = new File(target);
ImageIO.write(out, "jpg", t);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 得到放大或者縮小后的比例
*
* @param W
* 圖片原寬
* @param H
* 原高
* @param tarW
* 轉(zhuǎn)換后的寬
* @param zoom
* 放大還是縮小
* @return 返回寬和高的數(shù)組
*/
private static int[] getImageWidthAndHeight(int orgW, int orgH, int avW,
int avH) {
int width = 0;
int height = 0;
if (orgW > 0 && orgH > 0) {
if (orgW / orgH >= avW / avH) {
if (orgW > avW) {
width = avW;
height = (orgH * avW) / orgW;
} else {
width = orgW;
height = orgH;
}
System.out.println("++Widht:" + width + " Height" + height);
} else {
if (orgH > avH) {
height = avH;
width = (orgW * avH) / orgH;
} else {
width = orgW;
height = orgH;
}
System.out.println("++Widht:" + width + " Height" + height);
}
}
int[] arr = new int[2];
arr[0] = width;
arr[1] = height;
// long start = System.currentTimeMillis();
// int width = 0;
// int height = 0;
// if ((W / tarW) >= (H / tarH)) {// 寬的縮小比例大于高的
// width = tarW;
// height = H * tarW / W;
// System.out.println(width + " " + height);
// } else {
// height = tarH;
// width = W * tarH / H;
// System.out.println(width + " " + height);
// }
// int[] arr = new int[2];
// arr[0] = width;
// arr[1] = height;
// long end = System.currentTimeMillis();
// System.out.println("寬高處理:" + (end - start));
return arr;
}
public void picscale(String source, String target, int w, int h) {
File f = new File(source);
int width = 0;
int height = 0;
try {
BufferedImage bi = ImageIO.read(f);
int[] arr = getImageWidthAndHeight(bi.getWidth(), bi.getHeight(),
w, h);
width = arr[0];
height = arr[1];
BufferedImage out = null;
ImageScale scal = new ImageScale();
out = scal.imageZoomOut(bi, width, height);
File t = new File(target);
ImageIO.write(out, "jpg", t);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
*調(diào)用scale(源文件路徑,保存路徑,最大寬,最大高)
*
*
*/
public static void main(String[] args) {
ImageScale is = new ImageScale();
long start = System.currentTimeMillis();
is.scale("D:/nie.jpg", "D:/t6.jpg", 250, 194);
long end = System.currentTimeMillis();
System.out.println("時間:" + (end - start));
}
}

PS:這里再為大家推薦幾款比較實用的圖片處理工具供大家參考使用:

在線圖片轉(zhuǎn)換BASE64工具:
http://tools.jb51.net/transcoding/img2base64

ICO圖標(biāo)在線生成工具:
http://tools.jb51.net/aideddesign/ico_img

在線Email郵箱圖標(biāo)制作工具:
http://tools.jb51.net/email/emaillogo

在線圖片格式轉(zhuǎn)換(jpg/bmp/gif/png)工具:
http://tools.jb51.net/aideddesign/picext

更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java圖片操作技巧匯總》、《java日期與時間操作技巧匯總》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》及《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》。

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

相關(guān)文章

  • java中處理stream.filter()的實例代碼

    java中處理stream.filter()的實例代碼

    stream()是Java 8中的一個函數(shù)式接口,用于處理數(shù)據(jù)流,它可以從一個數(shù)據(jù)源,如集合,數(shù)組等生成一個流,這篇文章主要給大家介紹了關(guān)于java中處理stream.filter()的相關(guān)資料,需要的朋友可以參考下
    2024-08-08
  • SpringBoot+?Sharding?Sphere?輕松實現(xiàn)數(shù)據(jù)庫字段加解密功能

    SpringBoot+?Sharding?Sphere?輕松實現(xiàn)數(shù)據(jù)庫字段加解密功能

    在Spring?Boot生態(tài)中,有一個非常厲害的開源框架:Apache?ShardingSphere,它是一款分布式?SQL?事務(wù)和查詢引擎,可通過數(shù)據(jù)分片、彈性伸縮、加密等能力對任意數(shù)據(jù)庫進行增強,今天通過這篇文章,我們一起來了解一下如何在?Spring?Boot?中快速實現(xiàn)數(shù)據(jù)的加解密功能
    2024-07-07
  • SpringBoot集成極光推送的實現(xiàn)代碼

    SpringBoot集成極光推送的實現(xiàn)代碼

    工作中經(jīng)常會遇到服務(wù)器向App推送消息的需求,一般企業(yè)中選擇用極光推送的比較多,本文就介紹了SpringBoot集成極光推送的實現(xiàn)代碼,感興趣的可以了解一下
    2023-08-08
  • 深入淺出重構(gòu)Mybatis與Spring集成的SqlSessionFactoryBean(上)

    深入淺出重構(gòu)Mybatis與Spring集成的SqlSessionFactoryBean(上)

    通常來講,重構(gòu)是指不改變功能的情況下優(yōu)化代碼,但本文所說的重構(gòu)也包括了添加功能。這篇文章主要介紹了重構(gòu)Mybatis與Spring集成的SqlSessionFactoryBean(上)的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • JVM內(nèi)存區(qū)域劃分相關(guān)原理詳解

    JVM內(nèi)存區(qū)域劃分相關(guān)原理詳解

    這篇文章主要介紹了JVM內(nèi)存區(qū)域劃分相關(guān)原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • Java中的六種經(jīng)典比較排序算法

    Java中的六種經(jīng)典比較排序算法

    排序算法是程序開發(fā)和計算機科學(xué)中常見的算法之一,排序算法是算法分析的重要內(nèi)容之一,因為排序算法的效率影響著程序的性能和穩(wěn)定性,本文的目的是介紹常見的排序算法,并且通過代碼示例演示它們的實現(xiàn)過程,需要的朋友可以參考下
    2023-06-06
  • Spring使用@Value注解與@PropertySource注解加載配置文件操作

    Spring使用@Value注解與@PropertySource注解加載配置文件操作

    這篇文章主要介紹了Spring使用@Value注解與@PropertySource注解加載配置文件操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java之SpringBoot定時任務(wù)案例講解

    Java之SpringBoot定時任務(wù)案例講解

    這篇文章主要介紹了Java之SpringBoot定時任務(wù)案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Java中Maven Shade插件的具體使用

    Java中Maven Shade插件的具體使用

    Maven Shade插件它可以幫助你在構(gòu)建項目時打包所有依賴項,并將其打包到一個單獨的JAR文件中,本文就介紹一下Maven Shade插件的具體使用,具有一定參考價值,感興趣的可以了解一下
    2023-08-08
  • Java之Spring Bean 作用域和生命周期

    Java之Spring Bean 作用域和生命周期

    這篇文章主要介紹了Java Bean的作用域和生命周期,Bean 的作用域是指 Bean 在 Spring 整個框架中的某種行為模式,所謂的?命周期指的是?個對象從誕?到銷毀的整個?命過程,我們把這個過程就叫做?個對象的?命周期,感興趣的同學(xué)可以參考閱讀
    2023-04-04

最新評論