java操作图片.doc

上传人:桔**** 文档编号:549107628 上传时间:2023-03-17 格式:DOC 页数:38 大小:197.51KB
返回 下载 相关 举报
java操作图片.doc_第1页
第1页 / 共38页
java操作图片.doc_第2页
第2页 / 共38页
java操作图片.doc_第3页
第3页 / 共38页
java操作图片.doc_第4页
第4页 / 共38页
java操作图片.doc_第5页
第5页 / 共38页
点击查看更多>>
资源描述

《java操作图片.doc》由会员分享,可在线阅读,更多相关《java操作图片.doc(38页珍藏版)》请在金锄头文库上搜索。

1、package com.hy.grahpic;import java.awt.AlphaComposite;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Point;import java.awt.Rectangle;import java.awt.color.ColorSpace;import java.awt.image.BufferedImage;import java.awt.image.ColorConvert

2、Op;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Iterator;import java.util.List;import javax.imageio.ImageIO

3、;import javax.imageio.ImageReadParam;import javax.imageio.ImageReader;import javax.imageio.stream.ImageInputStream;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;/* * author zfwang * 修改日期 2015-9-7 * 使用技巧:注意参数即可、配置相关架包 */public class OperateImage public Ope

4、rateImage() super();/* * 对图片裁剪,并把裁剪新图片保存 * param srcPath 读取源图片路径 * param toPath写入图片路径 * param x 剪切起始点x坐标 * param y 剪切起始点y坐标 * param width 剪切宽度 * param height 剪切高度 * param readImageFormat 读取图片格式 * param writeImageFormat 写入图片格式 * throws IOException * 注意事项: */ public void cropImage(String srcPath,Strin

5、g toPath, int x,int y,int width,int height, String readImageFormat,String writeImageFormat) throws IOException FileInputStream fis = null ; ImageInputStream iis =null ; try /读取图片文件 fis = new FileInputStream(srcPath); Iterator it = ImageIO.getImageReadersByFormatName(readImageFormat); ImageReader rea

6、der = (ImageReader) it.next(); /获取图片流 iis = ImageIO.createImageInputStream(fis); reader.setInput(iis,true) ; ImageReadParam param = reader.getDefaultReadParam(); /定义一个矩形 Rectangle rect = new Rectangle(x, y, width, height); /提供一个 BufferedImage,将其用作解码像素数据的目标。 param.setSourceRegion(rect); BufferedImage

7、 bi = reader.read(0,param); /保存新图片 ImageIO.write(bi, writeImageFormat, new File(toPath); finally if(fis!=null) fis.close(); if(iis!=null) iis.close(); /* * 按倍率缩小图片 * param srcImagePath 读取图片路径 * param toImagePath 写入图片路径 * param widthRatio宽度缩小比例 * param heightRatio 高度缩小比例 * throws IOException * 注意事项:1

8、.缩小倍率必须1;2.jar包处理,在pom文件引入rt、jce两个架包(jdk安装后的lib中) org.apache.maven.plugins maven-compiler-plugin 2.3.2 $java-version $java-version UTF-8 $java.homelibrt.jar;$java.homelibjce.jar */ public void reduceImageByRatio(String srcImagePath,String toImagePath,int widthRatio,int heightRatio) throws IOExceptio

9、n FileOutputStream out = null; try /读入文件 File file = new File(srcImagePath); / 构造Image对象 BufferedImage src = javax.imageio.ImageIO.read(file); int width = src.getWidth(); int height = src.getHeight(); / 缩小边长 BufferedImage tag = new BufferedImage(width / widthRatio, height / heightRatio, BufferedImag

10、e.TYPE_INT_RGB); / 绘制 缩小 后的图片 tag.getGraphics().drawImage(src, 0, 0, width / widthRatio, height / heightRatio, null); out = new FileOutputStream(toImagePath); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); catch(Exception e) e.printStackTrace(); finally if(out != n

11、ull) out.close(); /* * 长高等比例缩小图片 * param srcImagePath 读取图片路径 * param toImagePath 写入图片路径 * param ratio 缩小比例 * throws IOException */ public void reduceImageEqualProportion(String srcImagePath,String toImagePath,int ratio) throws IOException FileOutputStream out = null; try /读入文件 File file = new File(srcImagePath); / 构造Image对象 BufferedImage src = javax.imageio.ImageIO.read(file); int width = src.getWidth(); int height = src.getHeight();

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号