缩略图两种方法
来源:互联网 发布:python wordcloud安装 编辑:程序博客网 时间:2024/06/04 22:47
方法一:
方法二:
import javax.imageio.ImageIO;
import javax.imageio.IIOException;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.io.File;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;
public class bbb {
public static void main (String argv[]) {
try {
File fi = new File("c:/image2.jpg"); //大图文件
File fo = new File("c:/imgTest.jpg"); //将要转换出的小图文件
int nw = 100;
/*
AffineTransform 类表示 2D 仿射变换,它执行从 2D 坐标到其他 2D
坐标的线性映射,保留了线的“直线性”和“平行性”。可以使用一系
列平移、缩放、翻转、旋转和剪切来构造仿射变换。
*/
AffineTransform transform = new AffineTransform();
BufferedImage bis = ImageIO.read(fi); //读取图片
int w = bis.getWidth();
int h = bis.getHeight();
//double scale = (double)w/h;
int nh = (nw*h)/w ;
double sx = (double)nw/w;
double sy = (double)nh/h;
transform.setToScale(sx,sy); //setToScale(double sx, double sy) 将此变换设置为缩放变换。
System.out.println(w + " " +h);
/*
* AffineTransformOp类使用仿射转换来执行从源图像或 Raster 中 2D 坐标到目标图像或
* Raster 中 2D 坐标的线性映射。所使用的插值类型由构造方法通过
* 一个 RenderingHints 对象或通过此类中定义的整数插值类型之一来指定。
如果在构造方法中指定了 RenderingHints 对象,则使用插值提示和呈现
的质量提示为此操作设置插值类型。要求进行颜色转换时,可以使用颜色
呈现提示和抖动提示。 注意,务必要满足以下约束:源图像与目标图像
必须不同。 对于 Raster 对象,源图像中的 band 数必须等于目标图像中
的 band 数。
*/
AffineTransformOp ato = new AffineTransformOp(transform,null);
BufferedImage bid = new BufferedImage(nw,nh,BufferedImage.TYPE_3BYTE_BGR);
/*
* TYPE_3BYTE_BGR 表示一个具有 8 位 RGB 颜色分量的图像,
* 对应于 Windows 风格的 BGR 颜色模型,具有用 3 字节存
* 储的 Blue、Green 和 Red 三种颜色。
*/
ato.filter(bis,bid);
ImageIO.write(bid,"jpeg",fo);
} catch(Exception e) {
e.printStackTrace();
}
}
}
import javax.imageio.IIOException;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.io.File;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;
public class bbb {
public static void main (String argv[]) {
try {
File fi = new File("c:/image2.jpg"); //大图文件
File fo = new File("c:/imgTest.jpg"); //将要转换出的小图文件
int nw = 100;
/*
AffineTransform 类表示 2D 仿射变换,它执行从 2D 坐标到其他 2D
坐标的线性映射,保留了线的“直线性”和“平行性”。可以使用一系
列平移、缩放、翻转、旋转和剪切来构造仿射变换。
*/
AffineTransform transform = new AffineTransform();
BufferedImage bis = ImageIO.read(fi); //读取图片
int w = bis.getWidth();
int h = bis.getHeight();
//double scale = (double)w/h;
int nh = (nw*h)/w ;
double sx = (double)nw/w;
double sy = (double)nh/h;
transform.setToScale(sx,sy); //setToScale(double sx, double sy) 将此变换设置为缩放变换。
System.out.println(w + " " +h);
/*
* AffineTransformOp类使用仿射转换来执行从源图像或 Raster 中 2D 坐标到目标图像或
* Raster 中 2D 坐标的线性映射。所使用的插值类型由构造方法通过
* 一个 RenderingHints 对象或通过此类中定义的整数插值类型之一来指定。
如果在构造方法中指定了 RenderingHints 对象,则使用插值提示和呈现
的质量提示为此操作设置插值类型。要求进行颜色转换时,可以使用颜色
呈现提示和抖动提示。 注意,务必要满足以下约束:源图像与目标图像
必须不同。 对于 Raster 对象,源图像中的 band 数必须等于目标图像中
的 band 数。
*/
AffineTransformOp ato = new AffineTransformOp(transform,null);
BufferedImage bid = new BufferedImage(nw,nh,BufferedImage.TYPE_3BYTE_BGR);
/*
* TYPE_3BYTE_BGR 表示一个具有 8 位 RGB 颜色分量的图像,
* 对应于 Windows 风格的 BGR 颜色模型,具有用 3 字节存
* 储的 Blue、Green 和 Red 三种颜色。
*/
ato.filter(bis,bid);
ImageIO.write(bid,"jpeg",fo);
} catch(Exception e) {
e.printStackTrace();
}
}
}
方法二:
import java.io.*;import java.awt.*;
import java.awt.image.*;import com.sun.image.codec.jpeg.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c)2007-6-13</p>
* <p>Company: fuen</p>
* @author 杨振朋
* @version 1.0
*/
public class ccc {
private String srcFile;
private String destFile;
private int width;
private int height;
private Image img;
/**
* 构造函数
* @param fileName String
* @throws IOException
*/
public ccc(String fileName) throws IOException {
File _file = new File(fileName); //读入文件
this.srcFile = _file.getName();
this.destFile = "c:/aa.jpg";//this.srcFile.substring(0, this.srcFile.lastIndexOf(".")) +"_s.jpg";
img = javax.imageio.ImageIO.read(_file); //构造Image对象
width = img.getWidth(null); //得到源图宽
height = img.getHeight(null); //得到源图长
} /**
* 强制压缩/放大图片到固定的大小
* @param w int 新宽度
* @param h int 新高度
* @throws IOException
*/
public void resize(int w, int h) throws IOException {
BufferedImage _image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB);
_image.getGraphics().drawImage(img, 0, 0, w, h, null); //绘制缩小后的图
FileOutputStream newimageout = new FileOutputStream(destFile); //输出到文件流
/*
* JPEGImageEncoder 将图像缓冲数据编码为 JPEG 数据流。该接口的用户应在 Raster
* 或 BufferedImage 中提供图像数据,在 JPEGEncodeParams 对象中设置必要的参数,
* 并成功地打开 OutputStream(编码 JPEG 流的目的流)。JPEGImageEncoder 接口可
* 将图像数据编码为互换的缩略 JPEG 数据流,该数据流将写入提供给编码器的 OutputStream 中。
注意:com.sun.image.codec.jpeg 包中的类并不属于核心 Java API。它们属于 Sun 发布的
JDK 和 JRE 产品的组成部分。虽然其它获得许可方可能选择发布这些类,但开发人员不能寄
希望于从非 Sun 实现的软件中得到它们。我们期望相同的功能最终可以在核心 API 或标准扩
展中得到。
*/
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimageout);
encoder.encode(_image); //近JPEG编码
newimageout.close();
} /**
* 按照固定的比例缩放图片
* @param t double 比例
* @throws IOException
*/
public void resize(double t) throws IOException {
int w = (int) (width * t);
int h = (int) (height * t);
resize(w, h);
} /**
* 以宽度为基准,等比例放缩图片
* @param w int 新宽度
* @throws IOException
*/
public void resizeByWidth(int w) throws IOException {
int h = (int) (height * w / width);
resize(w, h);
} /**
* 以高度为基准,等比例缩放图片
* @param h int 新高度
* @throws IOException
*/
public void resizeByHeight(int h) throws IOException {
int w = (int) (width * h / height);
resize(w, h);
} /**
* 按照最大高度限制,生成最大的等比例缩略图
* @param w int 最大宽度
* @param h int 最大高度
* @throws IOException
*/
public void resizeFix(int w, int h) throws IOException {
if (width / height > w / h) {
resizeByWidth(w);
}
else {
resizeByHeight(h);
}
} /**
* 设置目标文件名
* setDestFile
* @param fileName String 文件名字符串
*/
public void setDestFile(String fileName) throws Exception {
if (!fileName.endsWith(".jpg")) {
throw new Exception("Dest File Must end with \".jpg\".");
}
destFile = fileName;
} /**
* 获取目标文件名
* getDestFile
*/
public String getDestFile() {
return destFile;
} /**
* 获取图片原始宽度
* getSrcWidth
*/
public int getSrcWidth() {
return width;
}
/**
* 获取图片原始高度
* getSrcHeight
*/
public int getSrcHeight() {
return height;
}
/*
* 调用测试
*/
public static void main(String[] args) throws Exception {
ccc ccc = new ccc("c:/image2.jpg");
ccc.resizeFix(500, 300);
}
}
import java.awt.image.*;import com.sun.image.codec.jpeg.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c)2007-6-13</p>
* <p>Company: fuen</p>
* @author 杨振朋
* @version 1.0
*/
public class ccc {
private String srcFile;
private String destFile;
private int width;
private int height;
private Image img;
/**
* 构造函数
* @param fileName String
* @throws IOException
*/
public ccc(String fileName) throws IOException {
File _file = new File(fileName); //读入文件
this.srcFile = _file.getName();
this.destFile = "c:/aa.jpg";//this.srcFile.substring(0, this.srcFile.lastIndexOf(".")) +"_s.jpg";
img = javax.imageio.ImageIO.read(_file); //构造Image对象
width = img.getWidth(null); //得到源图宽
height = img.getHeight(null); //得到源图长
} /**
* 强制压缩/放大图片到固定的大小
* @param w int 新宽度
* @param h int 新高度
* @throws IOException
*/
public void resize(int w, int h) throws IOException {
BufferedImage _image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB);
_image.getGraphics().drawImage(img, 0, 0, w, h, null); //绘制缩小后的图
FileOutputStream newimageout = new FileOutputStream(destFile); //输出到文件流
/*
* JPEGImageEncoder 将图像缓冲数据编码为 JPEG 数据流。该接口的用户应在 Raster
* 或 BufferedImage 中提供图像数据,在 JPEGEncodeParams 对象中设置必要的参数,
* 并成功地打开 OutputStream(编码 JPEG 流的目的流)。JPEGImageEncoder 接口可
* 将图像数据编码为互换的缩略 JPEG 数据流,该数据流将写入提供给编码器的 OutputStream 中。
注意:com.sun.image.codec.jpeg 包中的类并不属于核心 Java API。它们属于 Sun 发布的
JDK 和 JRE 产品的组成部分。虽然其它获得许可方可能选择发布这些类,但开发人员不能寄
希望于从非 Sun 实现的软件中得到它们。我们期望相同的功能最终可以在核心 API 或标准扩
展中得到。
*/
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimageout);
encoder.encode(_image); //近JPEG编码
newimageout.close();
} /**
* 按照固定的比例缩放图片
* @param t double 比例
* @throws IOException
*/
public void resize(double t) throws IOException {
int w = (int) (width * t);
int h = (int) (height * t);
resize(w, h);
} /**
* 以宽度为基准,等比例放缩图片
* @param w int 新宽度
* @throws IOException
*/
public void resizeByWidth(int w) throws IOException {
int h = (int) (height * w / width);
resize(w, h);
} /**
* 以高度为基准,等比例缩放图片
* @param h int 新高度
* @throws IOException
*/
public void resizeByHeight(int h) throws IOException {
int w = (int) (width * h / height);
resize(w, h);
} /**
* 按照最大高度限制,生成最大的等比例缩略图
* @param w int 最大宽度
* @param h int 最大高度
* @throws IOException
*/
public void resizeFix(int w, int h) throws IOException {
if (width / height > w / h) {
resizeByWidth(w);
}
else {
resizeByHeight(h);
}
} /**
* 设置目标文件名
* setDestFile
* @param fileName String 文件名字符串
*/
public void setDestFile(String fileName) throws Exception {
if (!fileName.endsWith(".jpg")) {
throw new Exception("Dest File Must end with \".jpg\".");
}
destFile = fileName;
} /**
* 获取目标文件名
* getDestFile
*/
public String getDestFile() {
return destFile;
} /**
* 获取图片原始宽度
* getSrcWidth
*/
public int getSrcWidth() {
return width;
}
/**
* 获取图片原始高度
* getSrcHeight
*/
public int getSrcHeight() {
return height;
}
/*
* 调用测试
*/
public static void main(String[] args) throws Exception {
ccc ccc = new ccc("c:/image2.jpg");
ccc.resizeFix(500, 300);
}
}
0 0
- 缩略图两种方法
- java 图片缩略图的两种方法
- java 图片缩略图的两种方法
- iOS截取视频缩略图的两种方法
- 完美解决35dir获取网站缩略图问题(两种方法)
- IOS平台生成图片缩略图的两种方案
- IOS平台生成图片缩略图的两种方案
- IOS平台生成图片缩略图的两种方案
- 安卓获取图片缩略图的两种方式
- IOS平台生成图片缩略图的两种方案
- IOS平台生成图片缩略图的两种方案
- 安卓获取图片缩略图的两种方式
- Android加载缩略图的几种方法
- JAVA生成【缩略图】方法
- 生成缩略图方法2
- 获取缩略图封装方法
- 缩略图的方法
- win10关闭缩略图方法
- 撞库攻击:一场需要用户参与的持久战
- [LeetCode]***Binary Tree Level Order Traversal(查代码)
- margin:0 auto 和text-align:center的区别是什么???
- 生成缩略图
- 常量指针、指针常量、指向常量的指针常量
- 缩略图两种方法
- Android-mac下如何使用adb命令
- c#面向对象总结
- OpenWRT - ser2net 自启动、禁用console serial以便空出串口
- IE8中text-align:center偏左的解决办法
- Linux中LVM(逻辑盘卷管理)logical volume manager
- 用java界面实现根据当前系统时间定时更新二维码
- 黑马程序员学习_反射
- 用例图