




已阅读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年学历类自考合同法-领导科学参考题库含答案解析(5卷)
- 2025年学历类自考合同法-学前比较教育参考题库含答案解析(5卷)
- 城市商业广场租金收取协议
- 2025-2030咖啡师培训行业市场发展分析及前景趋势与投融资发展机会研究报告
- 养殖业投入与产出合作保障合同
- 标准合同模板及使用说明
- 2025昆明市官渡区曙光小学幸福邻里校区聘用制教师招聘(10人)备考考试试题及答案解析
- 广告设计与制作外包服务合同
- 乐至县2025年医疗卫生辅助岗人员招募工作(21人)备考考试题库附答案解析
- 2026年高考第一轮复习数学第01讲 导数的概念及其意义、导数的运算(复习课件)
- 涪陵殡葬管理办法
- 2025年工会财务知识竞赛考试题库及参考答案
- 《火力发电企业电力监控系统商用密码应用技术要求》
- 《城市轨道交通初期运营客流预测要求》编制说明
- 中国刑事警察学院2024研究生考试真题(附答案)
- GB/T 13925-2025铸造高锰钢金相检验
- 血压监测技术课件教学
- 2025年石家庄市市属国有企业招聘笔试考试试题(含答案)
- 肺恶性肿瘤死亡病例讨论
- 外研版(2024)八年级上册英语Unit 1-Unit 6全册课文对照语法填空+课文原文+中文翻译
评论
0/150
提交评论