java操作图片.doc_第1页
java操作图片.doc_第2页
java操作图片.doc_第3页
java操作图片.doc_第4页
java操作图片.doc_第5页
已阅读5页,还剩33页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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.ColorConvertOp;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;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 OperateImage() 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,String 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 reader = (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 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.缩小倍率必须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 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(); / 缩小边长 BufferedImage tag = new BufferedImage(width / widthRatio, height / heightRatio, BufferedImage.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 != null) 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(); / 缩小边长 BufferedImage tag = new BufferedImage(width / ratio, height / ratio, BufferedImage.TYPE_INT_RGB); / 绘制 缩小 后的图片 tag.getGraphics().drawImage(src, 0, 0, width / ratio, height / ratio, null); out = new FileOutputStream(toImagePath); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); catch(Exception e) e.printStackTrace(); finally if(out != null) out.close(); /* * 按倍率放大图片 * param srcImagePath 读取图形路径 * param toImagePath 写入入行路径 * param widthRatio宽度放大比例 * param heightRatio 高度放大比例 * throws IOException */ public void enlargementImageByRatio(String srcImagePath,String toImagePath,int widthRatio,int heightRatio) 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(); / 放大边长 BufferedImage tag = new BufferedImage(width * widthRatio, height * heightRatio, BufferedImage.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 != null) out.close(); /* * 长高等比例放大图片 * param srcImagePath 读取图形路径 * param toImagePath 写入入行路径 * param ratio放大比例 * throws IOException */ public void enlargementImageEqualProportion(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(); / 放大边长 BufferedImage tag = new BufferedImage(width * ratio, height * ratio, BufferedImage.TYPE_INT_RGB); /绘制放大后的图片 tag.getGraphics().drawImage(src, 0, 0, width * ratio, height * ratio, null); out = new FileOutputStream(toImagePath); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); catch(Exception e) e.printStackTrace(); finally if(out != null) out.close(); /* * 重置图形的边长大小 * param srcImagePath * param toImagePath * param width * param height * throws IOException */ public void resizeImage(String srcImagePath,String toImagePath,int width,int height) throws IOException FileOutputStream out = null; try /读入文件 File file = new File(srcImagePath); / 构造Image对象 BufferedImage src = javax.imageio.ImageIO.read(file); / 放大边长 BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); /绘制放大后的图片 tag.getGraphics().drawImage(src, 0, 0, width, height, null); out = new FileOutputStream(toImagePath); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); catch(Exception e) e.printStackTrace(); finally if(out != null) out.close(); /* * 横向拼接图片(两张) * param firstSrcImagePath 第一张图片的路径 * param secondSrcImagePath第二张图片的路径 * param imageFormat拼接生成图片的格式 * param toPath拼接生成图片的路径 */ public void joinImagesHorizontal(String firstSrcImagePath, String secondSrcImagePath,String imageFormat, String toPath) try /读取第一张图片 File fileOne = new File(firstSrcImagePath); BufferedImage imageOne = ImageIO.read(fileOne); int width = imageOne.getWidth();/图片宽度 int height = imageOne.getHeight();/图片高度 /从图片中读取RGB int imageArrayOne = new intwidth*height; imageArrayOne = imageOne.getRGB(0,0,width,height,imageArrayOne,0,width); /对第二张图片做相同的处理 File fileTwo = new File(secondSrcImagePath); BufferedImage imageTwo = ImageIO.read(fileTwo); int width2 = imageTwo.getWidth(); int height2 = imageTwo.getHeight(); int ImageArrayTwo = new intwidth2*height2; ImageArrayTwo = imageTwo.getRGB(0,0,width,height,ImageArrayTwo,0,width); /ImageArrayTwo = imageTwo.getRGB(0,0,width2,height2,ImageArrayTwo,0,width2); /生成新图片 /int height3 = (heightheight2 | height=height2)?height:height2; BufferedImage imageNew = new BufferedImage(width*2,height,BufferedImage.TYPE_INT_RGB); /BufferedImage imageNew = new BufferedImage(width+width2,height3,BufferedImage.TYPE_INT_RGB); imageNew.setRGB(0,0,width,height,imageArrayOne,0,width);/设置左半部分的RGB imageNew.setRGB(width,0,width,height,ImageArrayTwo,0,width);/设置右半部分的RGB /imageNew.setRGB(width,0,width2,height2,ImageArrayTwo,0,width2);/设置右半部分的RGB File outFile = new File(toPath); ImageIO.write(imageNew, imageFormat, outFile);/写图片 catch (Exception e) e.printStackTrace(); /* * 横向拼接一组(多张)图像 * param pics 将要拼接的图像 * param type 图像写入格式 * param dst_pic 图像写入路径 * return */ public boolean joinImageListHorizontal(String pics, String type, String dst_pic) try int len = pics.length; if (len 1) System.out.println(pics len 1); return false; File src = new Filelen; BufferedImage images = new BufferedImagelen; int imageArrays = new intlen; for (int i = 0; i len; i+) srci = new File(picsi); imagesi = ImageIO.read(srci); int width = imagesi.getWidth(); int height = imagesi.getHeight(); imageArraysi = new intwidth * height;/ 从图片中读取RGB imageArraysi = imagesi.getRGB(0, 0, width, height, imageArraysi, 0, width); int dst_width = 0; int dst_height = images0.getHeight(); for (int i = 0; i imagesi.getHeight() ? dst_height : imagesi.getHeight(); dst_width += imagesi.getWidth(); /System.out.println(dst_width); /System.out.println(dst_height); if (dst_height 1) System.out.println(dst_height 1); return false; /* * 生成新图片 */ BufferedImage ImageNew = new BufferedImage(dst_width, dst_height, BufferedImage.TYPE_INT_RGB); int width_i = 0; for (int i = 0; i width2 | width=width2)?width:width2; BufferedImage imageNew = new BufferedImage(width,height*2,BufferedImage.TYPE_INT_RGB); /BufferedImage imageNew = new BufferedImage(width3,height+height2,BufferedImage.TYPE_INT_RGB); imageNew.setRGB(0,0,width,height,imageArrayOne,0,width);/设置上半部分的RGB imageNew.setRGB(0,height,width,height,ImageArrayTwo,0,width);/设置下半部分的RGB /imageNew.setRGB(0,height,width2,height2,ImageArrayTwo,0,width2);/设置下半部分的RGB File outFile = new File(toPath); ImageIO.write(imageNew, imageFormat, outFile);/写图片 catch (Exception e) e.printStackTrace(); /* * 纵向拼接一组(多张)图像 * param pics将要拼接的图像数组 * param type写入图像类型 * param dst_pic写入图像路径 * return */public boolean joinImageListVertical(String pics, String type, String dst_pic) try int len = pics.length; if (len 1) System.out.println(pics len 1); return false; File src = new Filelen; BufferedImage images = new BufferedImagelen; int imageArrays = new intlen; for (int i = 0; i len; i+) /System.out.println(i); srci = new File(picsi); imagesi = ImageIO.read(srci); int width = imagesi.getWidth(); int height = imagesi.getHeight(); imageArraysi = new intwidth * height;/ 从图片中读取RGB imageArraysi = imagesi.getRGB(0, 0, width, height, imageArraysi, 0, width); int dst_height = 0; int dst_width = images0.getWidth(); for (int i = 0; i imagesi.getWidth() ? dst_width : imagesi.getWidth(); dst_height += imagesi.getHeight(); /System.out.println(dst_width); /System.out.println(dst_height); if (dst_height 1) System.out.println(dst_height 1); return false; /* * 生成新图片 */ BufferedImage ImageNew = new BufferedImage(dst_width, dst_height, BufferedImage.TYPE_INT_RGB); int height_i = 0; for (int i = 0; i images.length; i+) ImageNew.setRGB(0, height_i, dst_width, imagesi.getHeight(), imageArraysi, 0, dst_width); height_i += imagesi.getHeight(); File outFile = new File(dst_pic); ImageIO.write(ImageNew, type, outFile);/ 写图片 catch (Exception e) e.printStackTrace(); return false; return true; /* * 合并图片(按指定初始x、y坐标将附加图片贴到底图之上) * param negativeImagePath 背景图片路径 * param additionImagePath附加图片路径 * param x 附加图片的起始点x坐标 * param y 附加图片的起始点y坐标 * param toPath 图片写入路径 * throws IOException */ public void mergeBothImage(String negativeImagePath,String additionImagePath,int x,int y,String toPath ) throws IOException InputStream is= null; InputStream is2= null; OutputStream os = null; try is=new FileInputStream(negativeImagePath); is2=new FileInputStream(additionImagePath); BufferedImage image=ImageIO.read(is); BufferedImage image2=ImageIO.read(is2); Graphics g=image.getGraphics(); g.dr

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论