已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
该文档所编写的JAVA类,用于生成各种格式图片的缩略图。import java.awt.Image;import java.awt.Graphics2D;import java.awt.geom.AffineTransform;import java.awt.image.BufferedImage;import java.io.IOException;import java.io.OutputStream;import java.io.FileOutputStream;import javax.swing.ImageIcon;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;import java.io.FileNotFoundException;import java.awt.Toolkit;import java.io.BufferedOutputStream;import com.sun.image.codec.jpeg.JPEGEncodeParam;import java.awt.image.MemoryImageSource;import java.io.FileInputStream;import cn.js.fan.util.StrUtil;public class Thumbnail /* * Reads an image in a file and creates * a thumbnail in another file. * param orig The name of image file. * param thumb The name of thumbnail file. * Will be created if necessary. * param maxDim The width and height of * the thumbnail must * be maxDim pixels or less. */ public static void createThumbnail( String srcPath, String thumbPath, int maxDim) try / Get the image from a file. if (srcPath.toLowerCase().endsWith(.bmp) inImage = getBMPImage(srcPath); else inImage = new ImageIcon( srcPath).getImage(); / Determine the scale. double scale = (double) maxDim / ( double) inImage.getHeight(null); if (inImage.getWidth( null) inImage.getHeight(null) scale = (double) maxDim / ( double) inImage.getWidth(null); / Determine size of new image. /One of them / should equal maxDim. int scaledW = (int) ( scale * inImage.getWidth(null); int scaledH = (int) ( scale * inImage.getHeight(null); / Create an image buffer in /which to paint on. BufferedImage outImage = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_INT_RGB); / Set the scale. AffineTransform tx = new AffineTransform(); / If the image is smaller than / the desired image size, / dont bother scaling. if (scale 1.0d) tx.scale(scale, scale); / Paint image. Graphics2D g2d = outImage.createGraphics(); g2d.drawImage(inImage, tx, null); g2d.dispose(); / JPEG-encode the image /and write to file. OutputStream os = new FileOutputStream(thumbPath); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os); encoder.encode(outImage); os.close(); catch (IOException e) e.printStackTrace(); /* * 根据限定的宽度生成缩略图(会生成黑色背景) * param srcPath String * param thumbPath String * param widthDim int */ public static void createThumbnailLimitWidth( String srcPath, String thumbPath, int widthDim) try / Get the image from a file. Image inImage = null; if (srcPath.toLowerCase().endsWith(.bmp) inImage = getBMPImage(srcPath); else inImage = new ImageIcon( srcPath).getImage(); / Determine the scale. double scale = (double) widthDim / (double) inImage.getWidth(null); / Determine size of new image. /One of them / should equal maxDim. int scaledW = (int) ( scale * inImage.getWidth(null); int scaledH = (int) ( scale * inImage.getHeight(null); / Create an image buffer in / which to paint on. BufferedImage outImage = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_INT_RGB); / Set the scale. AffineTransform tx = new AffineTransform(); / If the image is smaller than / the desired image size, / dont bother scaling. / if (scale 1.0d) tx.scale(scale, scale); / / Paint image. Graphics2D g2d = outImage.createGraphics(); g2d.drawImage(inImage, tx, null); g2d.dispose(); / JPEG-encode the image /and write to file. OutputStream os = new FileOutputStream(thumbPath); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os); encoder.encode(outImage); os.close(); catch (IOException e) e.printStackTrace(); /* * 读取BMP图片 * param source String */ private static Image getBMPImage(String source) throws Exception FileInputStream fs = null; Image image = null; try fs = new FileInputStream(source); int bfLen = 14; byte bf = new bytebfLen; fs.read(bf, 0, bfLen); / 读取14字节BMP文件头 int biLen = 40; byte bi = new bytebiLen; fs.read(bi, 0, biLen); / 读取40字节BMP信息头 / 源图宽度 int nWidth = (int) bi7 & 0xff) 24) | (int) bi6 & 0xff) 16) | (int) bi5 & 0xff) 8) | (int) bi4 & 0xff; / 源图高度 int nHeight = (int) bi11 & 0xff) 24) | (int) bi10 & 0xff) 16) | (int) bi9 & 0xff) 8) | (int) bi8 & 0xff; / 位数 int nBitCount = (int) bi15 & 0xff) 8) | (int) bi14 & 0xff; / 源图大小 int nSizeImage = (int) bi23 & 0xff) 24) | (int) bi22 & 0xff) 16) | (int) bi21 & 0xff) 8) | (int) bi20 & 0xff; / 对24位BMP进行解析 if (nBitCount = 24) int nPad = (nSizeImage / nHeight) - nWidth * 3; int nData = new intnHeight * nWidth; byte bRGB = new byte(nWidth + nPad) * 3 * nHeight; fs.read(bRGB, 0, (nWidth + nPad) * 3 * nHeight); int nIndex = 0; for (int j = 0; j nHeight; j+) for (int i = 0; i nWidth; i+) nDatanWidth * (nHeight - j - 1) + i = (255 & 0xff) 24 | (int) bRGBnIndex + 2 & 0xff) 16) | (int) bRGBnIndex + 1 & 0xff) 8) | (int) bRGBnIndex & 0xff; nIndex += 3; nIndex += nPad; Toolkit kit = T
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 信用评价服务协议书
- 赛博协议书攻略
- 人员替换协议书范本
- 2025年RISC-VMatter协议芯片设计考核试卷
- 2025年RCEP项下纺织面料原产地规则应用考核试卷
- 其协议书有效
- 山东铁路补亏协议书
- 人才结对帮扶协议书
- 租赁到期解除协议书
- 2025年监理工程师(土木建筑工程)《施工质量控制》历年真题历年真题实战预测详解卷(含智能建造质量验收标准)考核试卷
- 二十届四中全会测试题及参考答案
- 华润燃气管理能力测评题库及答案详解
- 23G409先张法预应力混凝土管桩
- 提升基层应急能力筑牢防灾减灾救灾人民防线课件
- 企业所得税汇算清缴政策辅导-课件
- 血细胞形态图库
- 建筑消防设施故障维修记录表
- 标准解法体系(5级共76个标准解)
- 完整版天丝织物的染整工艺
- 变电站视频监控系统施工方案
- 聚丙烯工艺综述ppt课件
评论
0/150
提交评论