java如何实现缩略图,Java实现缩略图的步骤

Java实现缩略图的方法

赵永恩

import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;

import java.awt.image.ColorModel;

import java.awt.image.WritableRaster;

import java.awt.*;

import java.awt.geom.AffineTransform;

import java.io.InputStream;

import java.io.File;

import java.io.FileOutputStream;

public class Test {

public static BufferedImage resize(BufferedImage source, int targetW, int targetH) {

// targetW,targetH分别表示目标长和宽

int type = source.getType();

BufferedImage target = null;

double sx = (double) targetW / source.getWidth();

double sy = (double) targetH / source.getHeight();

//这里想实现在targetW,targetH范围内实现等比缩放。如果不需要等比缩放

//则将下面的if else语句注释即可

if(sx>sy)

{

sx = sy;

targetW = (int)(sx * source.getWidth());

}else{

sy = sx;

targetH = (int)(sy * source.getHeight());

}

if (type == BufferedImage.TYPE_CUSTOM) { //handmade

ColorModel cm = source.getColorModel();

WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);

boolean alphaPremultiplied = cm.isAlphaPremultiplied();

target = new BufferedImage(cm, raster, alphaPremultiplied, null);

} else

target = new BufferedImage(targetW, targetH, type);

Graphics2D g = target.createGraphics();

//smoother than exlax:

g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY );

g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));

g.dispose();

return target;

}

public static void saveImageAsJpg (String fromFileStr,String saveToFileStr,int width,int hight)

throws Exception {

BufferedImage srcImage;

// String ex = fromFileStr.substring(fromFileStr.indexOf("."),fromFileStr.length());

String imgType = "JPEG";

if (fromFileStr.toLowerCase().endsWith(".png")) {

imgType = "PNG";

}

// System.out.println(ex);

File saveFile=new File(saveToFileStr);

File fromFile=new File(fromFileStr);

srcImage = ImageIO.read(fromFile);

if(width > 0 || hight > 0)

{

srcImage = resize(srcImage, width, hight);

}

ImageIO.write(srcImage, imgType, saveFile);

}

public static void main (String argv[]) {

try{

//参数1(from),参数2(to),参数3(宽),参数4(高)

Test.saveImageAsJpg("E:/Document/My Pictures/3.gif","c:/6.gif",50,50);

} catch(Exception e)

{

e.printStackTrace();

}

}

}

import java.io.*;

import java.util.*;

import com.sun.image.codec.jpeg.*;

import java.awt.image.*;

import java.awt.*;

import java.net.*;

import java.applet.*;

import java.sql.*;

//缩略图类,

//本java类能将jpg图片文件,进行等比或非等比的大小转换。

//具体使用方法

//s_pic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))

public class Tes {

String InputDir; //输入图路径

String OutputDir; //输出图路径

String InputFileName; //输入图文件名

String OutputFileName; //输出图文件名

int OutputWidth = 80; //默认输出图片宽

int OutputHeight = 80; //默认输出图片高

int rate = 0;

boolean proportion = true; //是否等比缩放标记(默认为等比缩放)

public Tes() {

//初始化变量

InputDir = "";

OutputDir = "";

InputFileName = "";

OutputFileName = "";

OutputWidth = 80;

OutputHeight = 80;

rate = 0;

}

public void setInputDir(String InputDir) {

this.InputDir = InputDir;

}

public void setOutputDir(String OutputDir) {

this.OutputDir = OutputDir;

}

public void setInputFileName(String InputFileName) {

this.InputFileName = InputFileName;

}

public void setOutputFileName(String OutputFileName) {

this.OutputFileName = OutputFileName;

}

public void setOutputWidth(int OutputWidth) {

this.OutputWidth = OutputWidth;

}

public void setOutputHeight(int OutputHeight) {

this.OutputHeight = OutputHeight;

}

public void setW_H(int width, int height) {

this.OutputWidth = width;

this.OutputHeight = height;

}

public String s_pic() {

BufferedImage image;

String NewFileName;

//建立输出文件对象

File file = new File(OutputDir + OutputFileName);

FileOutputStream tempout = null;

try {

tempout = new FileOutputStream(file);

} catch (Exception ex) {

System.out.println(ex.toString());

}

Image img = null;

Toolkit tk = Toolkit.getDefaultToolkit();

Applet app = new Applet();

MediaTracker mt = new MediaTracker(app);

try {

img = tk.getImage(InputDir + InputFileName);

mt.addImage(img, 0);

mt.waitForID(0);

} catch (Exception e) {

e.printStackTrace();

}

if (img.getWidth(null) == -1) {

System.out.println(" can't read,retry!" + "
");

return "no";

} else {

int new_w;

int new_h;

if (this.proportion == true) { //判断是否是等比缩放.

//为等比缩放计算输出的图片宽度及高度

double rate1 = ((double) img.getWidth(null)) /

(double) OutputWidth + 0.1;

double rate2 = ((double) img.getHeight(null)) /

(double) OutputHeight + 0.1;

double rate = rate1 > rate2 ? rate1 : rate2;

new_w = (int) (((double) img.getWidth(null)) / rate);

new_h = (int) (((double) img.getHeight(null)) / rate);

} else {

new_w = OutputWidth; //输出的图片宽度

new_h = OutputHeight; //输出的图片高度

}

BufferedImage buffImg = new BufferedImage(new_w, new_h,

BufferedImage.TYPE_INT_RGB);

Graphics g = buffImg.createGraphics();

g.setColor(Color.white);

g.fillRect(0, 0, new_w, new_h);

g.drawImage(img, 0, 0, new_w, new_h, null);

g.dispose();

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(tempout);

try {

encoder.encode(buffImg);

tempout.close();

} catch (IOException ex) {

System.out.println(ex.toString());

}

}

return "ok";

}

public String s_pic(String InputDir, String OutputDir, String InputFileName,

String OutputFileName) {

//输入图路径

this.InputDir = InputDir;

//输出图路径

this.OutputDir = OutputDir;

//输入图文件名

this.InputFileName = InputFileName;

//输出图文件名

this.OutputFileName = OutputFileName;

return s_pic();

}

public String s_pic(String InputDir, String OutputDir, String InputFileName,

String OutputFileName, int width, int height,

boolean gp) {

//输入图路径

this.InputDir = InputDir;

//输出图路径

this.OutputDir = OutputDir;

//输入图文件名

this.InputFileName = InputFileName;

//输出图文件名

this.OutputFileName = OutputFileName;

//设置图片长宽

setW_H(width, height);

//是否是等比缩放 标记

this.proportion = gp;

return s_pic();

}

public static void main(String[] a) {

//s_pic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度)

Tes mypic = new Tes();

System.out.println(

mypic.s_pic("E:\Document\My Pictures\",

"E:\Document\My Pictures\",

"topbg-3.gif", "3.gif", 400, 400, true)

);

}

}

3.jsp方式

java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*,

try

{

java.io.File file = new java.io.File("E:\Document\My Pictures\3.gif");

String newurl="E:\Document\My Pictures\32.gif"; //新的缩略图保存地址

Image src = javax.imageio.ImageIO.read(file); //构造Image对象

float tagsize=200;

int old_w=src.getWidth(null); //得到源图宽

int old_h=src.getHeight(null);

int new_w=0;

int new_h=0; //得到源图长

int tempsize;

float tempdouble;

if(old_w>old_h){

tempdouble=old_w/tagsize;

}else{

tempdouble=old_h/tagsize;

}

new_w=Math.round(old_w/tempdouble);

new_h=Math.round(old_h/tempdouble);//计算新图长宽

BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);

tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //绘制缩小后的图

FileOutputStream newimage=new FileOutputStream(newurl); //输出到文件流

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);

encoder.encode(tag); //近JPEG编码

newimage.close();

}catch (Exception e){

e.toString();

}

这是其他地方看来的

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/505126.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

m 文件 dll matlab 中调用_如何在matlab中调用python程序

现在python很火,很多代码都是python写的,如果你和我一样,习惯了使用matlab,还想在matlab中调用Python的代码,应该怎么办呢?其中一条思路:首先在matlab中调用系统脚本命令,然后再通过…

spring整合atomikos实现分布式事务的方法示例_分布式事务中的XA和JTA

在介绍这两个概念之前,我们先看看是什么是X/Open DTP模型。X/Open X/Open,即现在的open group,是一个独立的组织,主要负责制定各种行业技术标准。X/Open组织主要由各大知名公司或者厂商进行支持,这些组织不光遵循X/Ope…

nginx php pathinfo,Nginx解决PATH_INFO新解决办法

在Nginx的0.7.31版本以前对pathinfo支持的很不好,需要各种设置,比如Thinkphp就配置很复杂,老版本配置可以参考惠新宸的blog《Nginx(PHP/fastcgi)的PATH_INFO问题》自从0.7.31以后,大家就有福了,我以Thinkphp为例&#…

hadoop 传感器数据_读取模式错误,计算引擎操作复杂……面对Hadoop这些问题该如何应对?...

作者 | Monte Zweben译者 | 天道酬勤,责编 | Carol封图 | CSDN 付费下载自视觉中国Apache Hadoop于2006年出现在IT领域,它使用商品硬件,为组织提供前所未有的数据量存储能力。不仅解决了数据集的大小问题,还解决了数据类型问题&am…

python怎么读取石墨表格_Python用Pandas读写Excel

Pandas是python的一个数据分析包,纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具。Pandas提供了大量能使我们快速便捷地处理数据的函数和方法。读者福利,也有安装包,想要了解python人工智能可直接点击链…

数值分析牛顿法 matlab,科学网—数值分析--非线性函数牛顿迭代法matlab程序 - 殷春武的博文...

%%%程序编写者 西北工业大学自动化学院 Email: yincwxa2013mail.nwpu.edu.cn%% All rights reservedclearclcx0input(输入迭代初值x0)syms xfinput(输入函数f(x))dfdiff(f)t1;epucinput(输入精度要求eupc)if epuc>0epucepuc;else epuc0.000001end%牛顿迭代法…

xpath获取标签的属性值_论xpath与css定位方式

例1&#xff1a;<input id"kw" name"wd" class"s_ipt" value"" maxlength"255" autocomplete"off"> 分别使用xpath、css的标签组合定位xpath标签属性组合定位css标签属性组合定位css中#表示id,如#kw&#x…

c语言sort_C语言十大排序算法,让老师对你刮目相看的技巧

排序算法作为数据结构的重要部分&#xff0c;系统地学习一下是很有必要的。十种常见排序算法可以分为两大类&#xff1a;比较类排序&#xff1a;通过比较来决定元素间的相对次序&#xff0c;由于其时间复杂度不能突破O(nlogn)&#xff0c;因此也称为非线性时间比较类排序。非比…

vim复制粘贴_打造一款高逼格的Vim神器

Vim 是一个上古神器&#xff0c;本篇文章主要持续总结使用 Vim 的过程中不得不了解的一些指令和注意事项&#xff0c;以及持续分享一个开发者不得不安装的一些插件&#xff0c;而关于 Vim 的简介&#xff0c;主题的选择&#xff0c;以及为何使用 vim-plug 来管理插件等内容&…

php 两个二维数组对比,php比较两二维数组求大神指教

如&#xff1a;$grade Array([0] > Array([course_name] > 大学英语(综合)-1[course_strid] > College English (Comprehensive)-1[credit] > 2[course_nature] > 必修[grade] > 70.0[year] > 2013-2014[term] > 1[user_id] > 1034))$temp Array(…

python列表嵌套字典取值_我的 python 学习历程-Day05 字典/字典的嵌套

一、字典的初识为什么要有字典字典与列表同属容器型数据类型&#xff0c;同样可以存储大量的数据&#xff0c;但是&#xff0c;列表的数据关联性不强&#xff0c;并且查询速度比较慢&#xff0c;只能按照顺序存储。什么是字典先说一下什么叫可变与不可变的数据类型分类不可变&a…

s8050三极管经典电路_曝光一个产品级的红外发射电路

作者&#xff1a;瑞生&#xff0c;来源&#xff1a;科技老顽童微信公众号&#xff1a;芯片之家(ID&#xff1a;chiphome-dy今天给大家一个产品级的红外发射电路。为什么说是产品级的&#xff1f;因为这个电路我已经在各类产品上见过多次&#xff01;很多小伙伴学电子有一个误区…

树莓派 python spi,树莓派测试SPI-基于设备操作ioctl

用于测试树莓派的SPI接口是否正常工作&#xff0c;代码来自于https://raw.githubusercontent.com/raspberrypi/Linux/rpi-3.10.y/Documentation/spi/spidev_test.c测试方法参考自&#xff1a;http://louisthiery.com/spi-Python-hardware-spi-for-raspi//** SPI testing utilit…

es6删除数组某一项_精学手撕系列——数组扁平化

参考文章&#xff1a;面试官连环追问&#xff1a;数组拍平(扁平化) flat 方法实现编者荐语&#xff1a;在前端面试中&#xff0c;手写flat是非常基础的面试题&#xff0c;通常出现在笔试或者第一轮面试中&#xff0c;主要考察面试者基本的手写代码能力和JavaScript的基本功。今…

iqc工作职责和工作内容_监理工程师工作职责

1&#xff0e;安全监理员是项目安全生产日常监理工作的主要实施者&#xff0c;代表总监理工程师在项目工程监理过程中行使项目安全生产监理的职责。2&#xff0e;安全监理员应认真贯彻执行《建设工程安全生产管理条例》&#xff0c;贯彻执行劳动保护、安全生产的方针政策、法令…

php 类常量用法,php类常量用法实例分析

这篇文章主要介绍了php类常量用法,实例分析了php中类常量的概念、特性与相关使用技巧,需要的朋友可以参考下本文实例讲述了php类常量用法。分享给大家供大家参考。具体如下&#xff1a;;echo Foo::BAR, ;$obj new Foo();echo $obj->getConstant(), ;echo $obj->getConst…

webservice 实现与his系统对接_[Share] EDI 及其他常见系统对接技术

近期&#xff0c;有客户提及&#xff1a;你们有没有对接技术相关的介绍&#xff0c;不同系统之间的对接技术&#xff0c;现在企业内部系统比较多&#xff0c;有自主开发的&#xff0c;有外部采购的&#xff0c;所以我们想了解一下对接技术相关的信息。小知马不停蹄的做了下功课…

php弱类型漏洞,php代码审计之弱类型引发的灾难

天融信阿尔法实验室 李喆有人说php是世界上最好的语言&#xff0c;这可能是对开发人员来说&#xff0c;确实有这方面的特点&#xff0c;因为它开发起来不像其他语言那样麻烦&#xff0c;就比如&#xff1a;弱类型&#xff0c;它不需要像java等语言那样明确定义数据类型。这给开…

大学计算机基础python第二次作业_第二次python作业-titanic数据练习

原博文 2019-10-14 14:45 − 一、读入titanic.xlsx文件&#xff0c;按照教材示例步骤&#xff0c;完成数据清洗。 titanic数据集包含11个特征&#xff0c;分别是&#xff1a; Survived:0代表死亡&#xff0c;1代表存活Pclass:乘客所持票类&#xff0c;有三种值(1,2,3)Name:乘客…

微小宝公众号排行榜_榜单 广东省技工院校微信公众号排行榜(第51期)

▼2020年10月榜单出炉&#xff01;本期榜单最大看点&#xff0c;莫过于发文33篇的劳模代表——“广东高新技术高级技校”以高达830.49的微信传播指数WCI&#xff0c;荣登第51期广东省技工院校微信公众号排行榜榜首&#xff01;截至本期&#xff0c;实力一向强劲的“广东高新技术…