图片上传

来源:互联网 发布:java 服务器文件同步 编辑:程序博客网 时间:2024/06/07 03:16


征文 | 从高考,到程序员      深度学习与TensorFlow入门一课搞定!      CSDN日报20170614 ——《一个 5 年程序员的回忆及建议》
 

Java imageIO处理图像

标签: javanullimagedstosstring
 43626人阅读 评论(1) 收藏 举报
  1. package com.adam.dev.pic.easyImage;  
  2. import java.awt.AlphaComposite;  
  3. import java.awt.Color;  
  4. import java.awt.Font;  
  5. import java.awt.Graphics;  
  6. import java.awt.Graphics2D;  
  7. import java.awt.Point;  
  8. import java.awt.Rectangle;  
  9. import java.awt.color.ColorSpace;  
  10. import java.awt.image.BufferedImage;  
  11. import java.awt.image.ColorConvertOp;  
  12. import java.io.BufferedReader;  
  13. import java.io.File;  
  14. import java.io.FileInputStream;  
  15. import java.io.FileOutputStream;  
  16. import java.io.FileReader;  
  17. import java.io.IOException;  
  18. import java.io.InputStream;  
  19. import java.io.OutputStream;  
  20. import java.util.Iterator;  
  21. import java.util.List;  
  22.   
  23. import javax.imageio.ImageIO;  
  24. import javax.imageio.ImageReadParam;  
  25. import javax.imageio.ImageReader;  
  26. import javax.imageio.stream.ImageInputStream;  
  27.   
  28. import com.sun.image.codec.jpeg.JPEGCodec;  
  29. import com.sun.image.codec.jpeg.JPEGImageEncoder;  
  30.   
  31. /** 
  32.  * @author adam.胡升阳 
  33.  * 创建日期 2012-2-29 
  34.  */  
  35. public class OperateImage{  
  36.   
  37.     public OperateImage() {  
  38.         super();  
  39.     }  
  40.   
  41.     /**  
  42.      * 对图片裁剪,并把裁剪新图片保存  
  43.      * @param srcPath 读取源图片路径 
  44.      * @param toPath    写入图片路径 
  45.      * @param x 剪切起始点x坐标 
  46.      * @param y 剪切起始点y坐标 
  47.      * @param width 剪切宽度 
  48.      * @param height     剪切高度 
  49.      * @param readImageFormat  读取图片格式 
  50.      * @param writeImageFormat 写入图片格式 
  51.      * @throws IOException 
  52.      */  
  53.     public void cropImage(String srcPath,String toPath,  
  54.             int x,int y,int width,int height,  
  55.             String readImageFormat,String writeImageFormat) throws IOException{     
  56.         FileInputStream fis = null ;  
  57.         ImageInputStream iis =null ;  
  58.         try{     
  59.             //读取图片文件   
  60.             fis = new FileInputStream(srcPath);   
  61.             Iterator it = ImageIO.getImageReadersByFormatName(readImageFormat);   
  62.             ImageReader reader = (ImageReader) it.next();   
  63.             //获取图片流    
  64.             iis = ImageIO.createImageInputStream(fis);    
  65.             reader.setInput(iis,true) ;  
  66.             ImageReadParam param = reader.getDefaultReadParam();   
  67.             //定义一个矩形   
  68.             Rectangle rect = new Rectangle(x, y, width, height);   
  69.             //提供一个 BufferedImage,将其用作解码像素数据的目标。    
  70.             param.setSourceRegion(rect);  
  71.             BufferedImage bi = reader.read(0,param);                  
  72.             //保存新图片    
  73.             ImageIO.write(bi, writeImageFormat, new File(toPath));       
  74.         }finally{  
  75.             if(fis!=null)  
  76.                 fis.close();         
  77.             if(iis!=null)  
  78.                iis.close();   
  79.         }   
  80.     }  
  81.   
  82.     /** 
  83.      * 按倍率缩小图片 
  84.      * @param srcImagePath 读取图片路径 
  85.      * @param toImagePath 写入图片路径 
  86.      * @param widthRatio    宽度缩小比例 
  87.      * @param heightRatio    高度缩小比例 
  88.      * @throws IOException 
  89.      */  
  90.     public void reduceImageByRatio(String srcImagePath,String toImagePath,int widthRatio,int heightRatio) throws IOException{  
  91.         FileOutputStream out = null;  
  92.         try{  
  93.             //读入文件     
  94.             File file = new File(srcImagePath);    
  95.             // 构造Image对象     
  96.             BufferedImage src = javax.imageio.ImageIO.read(file);    
  97.             int width = src.getWidth();    
  98.             int height = src.getHeight();    
  99.             // 缩小边长    
  100.             BufferedImage tag = new BufferedImage(width / widthRatio, height / heightRatio, BufferedImage.TYPE_INT_RGB);    
  101.             // 绘制 缩小  后的图片    
  102.             tag.getGraphics().drawImage(src, 00, width / widthRatio, height / heightRatio, null);    
  103.             out = new FileOutputStream(toImagePath);    
  104.             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);    
  105.             encoder.encode(tag);    
  106.         }catch(Exception e){  
  107.             e.printStackTrace();  
  108.         }finally{  
  109.             if(out != null){  
  110.                 out.close();    
  111.             }  
  112.         }  
  113.     }  
  114.   
  115.     /** 
  116.      * 长高等比例缩小图片 
  117.      * @param srcImagePath 读取图片路径 
  118.      * @param toImagePath 写入图片路径 
  119.      * @param ratio 缩小比例 
  120.      * @throws IOException 
  121.      */  
  122.     public void reduceImageEqualProportion(String srcImagePath,String toImagePath,int ratio) throws IOException{  
  123.         FileOutputStream out = null;  
  124.         try{  
  125.             //读入文件     
  126.             File file = new File(srcImagePath);    
  127.             // 构造Image对象     
  128.             BufferedImage src = javax.imageio.ImageIO.read(file);    
  129.             int width = src.getWidth();    
  130.             int height = src.getHeight();    
  131.             // 缩小边长    
  132.             BufferedImage tag = new BufferedImage(width / ratio, height / ratio, BufferedImage.TYPE_INT_RGB);    
  133.             // 绘制 缩小  后的图片    
  134.             tag.getGraphics().drawImage(src, 00, width / ratio, height / ratio, null);    
  135.             out = new FileOutputStream(toImagePath);    
  136.             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);    
  137.             encoder.encode(tag);    
  138.         }catch(Exception e){  
  139.             e.printStackTrace();  
  140.         }finally{  
  141.             if(out != null){  
  142.                 out.close();    
  143.             }  
  144.         }  
  145.     }  
  146.       
  147.     /** 
  148.      * 按倍率放大图片 
  149.      * @param srcImagePath 读取图形路径 
  150.      * @param toImagePath 写入入行路径 
  151.      * @param widthRatio    宽度放大比例 
  152.      * @param heightRatio 高度放大比例 
  153.      * @throws IOException 
  154.      */  
  155.     public void enlargementImageByRatio(String srcImagePath,String toImagePath,int widthRatio,int heightRatio) throws IOException{  
  156.         FileOutputStream out = null;  
  157.         try{  
  158.             //读入文件     
  159.             File file = new File(srcImagePath);    
  160.             // 构造Image对象     
  161.             BufferedImage src = javax.imageio.ImageIO.read(file);    
  162.             int width = src.getWidth();    
  163.             int height = src.getHeight();    
  164.             // 放大边长   
  165.             BufferedImage tag = new BufferedImage(width * widthRatio, height * heightRatio, BufferedImage.TYPE_INT_RGB);    
  166.             //绘制放大后的图片   
  167.             tag.getGraphics().drawImage(src, 00, width * widthRatio, height * heightRatio, null);    
  168.             out = new FileOutputStream(toImagePath);    
  169.             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);    
  170.             encoder.encode(tag);    
  171.         }catch(Exception e){  
  172.             e.printStackTrace();  
  173.         }finally{  
  174.             if(out != null){  
  175.                 out.close();    
  176.             }  
  177.         }  
  178.     }  
  179.       
  180.       
  181.     /** 
  182.      * 长高等比例放大图片 
  183.      * @param srcImagePath 读取图形路径 
  184.      * @param toImagePath 写入入行路径 
  185.      * @param ratio 放大比例 
  186.      * @throws IOException 
  187.      */  
  188.     public void enlargementImageEqualProportion(String srcImagePath,String toImagePath,int ratio) throws IOException{  
  189.         FileOutputStream out = null;  
  190.         try{  
  191.             //读入文件     
  192.             File file = new File(srcImagePath);    
  193.             // 构造Image对象     
  194.             BufferedImage src = javax.imageio.ImageIO.read(file);    
  195.             int width = src.getWidth();    
  196.             int height = src.getHeight();    
  197.             // 放大边长   
  198.             BufferedImage tag = new BufferedImage(width * ratio, height * ratio, BufferedImage.TYPE_INT_RGB);    
  199.             //绘制放大后的图片   
  200.             tag.getGraphics().drawImage(src, 00, width * ratio, height * ratio, null);    
  201.             out = new FileOutputStream(toImagePath);    
  202.             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);    
  203.             encoder.encode(tag);    
  204.         }catch(Exception e){  
  205.             e.printStackTrace();  
  206.         }finally{  
  207.             if(out != null){  
  208.                 out.close();    
  209.             }  
  210.         }  
  211.     }  
  212.       
  213.     /** 
  214.      * 重置图形的边长大小 
  215.      * @param srcImagePath  
  216.      * @param toImagePath 
  217.      * @param width 
  218.      * @param height 
  219.      * @throws IOException 
  220.      */  
  221.     public void resizeImage(String srcImagePath,String toImagePath,int width,int height) throws IOException{  
  222.         FileOutputStream out = null;  
  223.         try{  
  224.             //读入文件     
  225.             File file = new File(srcImagePath);    
  226.             // 构造Image对象     
  227.             BufferedImage src = javax.imageio.ImageIO.read(file);    
  228.             // 放大边长   
  229.             BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);    
  230.             //绘制放大后的图片   
  231.             tag.getGraphics().drawImage(src, 00, width, height, null);    
  232.             out = new FileOutputStream(toImagePath);    
  233.             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);    
  234.             encoder.encode(tag);    
  235.         }catch(Exception e){  
  236.             e.printStackTrace();  
  237.         }finally{  
  238.             if(out != null){  
  239.                 out.close();    
  240.             }  
  241.         }  
  242.     }  
  243.       
  244.     /** 
  245.      * 横向拼接图片(两张) 
  246.      * @param firstSrcImagePath 第一张图片的路径 
  247.      * @param secondSrcImagePath    第二张图片的路径 
  248.      * @param imageFormat   拼接生成图片的格式 
  249.      * @param toPath    拼接生成图片的路径 
  250.      */  
  251.     public void joinImagesHorizontal(String firstSrcImagePath, String secondSrcImagePath,String imageFormat, String toPath){    
  252.         try {    
  253.             //读取第一张图片       
  254.             File  fileOne  =  new  File(firstSrcImagePath);      
  255.             BufferedImage  imageOne = ImageIO.read(fileOne);      
  256.             int  width  =  imageOne.getWidth();//图片宽度       
  257.             int  height  =  imageOne.getHeight();//图片高度       
  258.             //从图片中读取RGB       
  259.             int[]  imageArrayOne  =  new  int[width*height];      
  260.             imageArrayOne  =  imageOne.getRGB(0,0,width,height,imageArrayOne,0,width);      
  261.              
  262.             //对第二张图片做相同的处理       
  263.             File  fileTwo  =  new  File(secondSrcImagePath);      
  264.             BufferedImage  imageTwo  =  ImageIO.read(fileTwo);   
  265.             int width2 = imageTwo.getWidth();  
  266.             int height2 = imageTwo.getHeight();  
  267.             int[]   ImageArrayTwo  =  new  int[width2*height2];      
  268.             ImageArrayTwo  =  imageTwo.getRGB(0,0,width,height,ImageArrayTwo,0,width);      
  269.             //ImageArrayTwo  =  imageTwo.getRGB(0,0,width2,height2,ImageArrayTwo,0,width2);    
  270.              
  271.             //生成新图片   
  272.             //int height3 = (height>height2 || height==height2)?height:height2;   
  273.             BufferedImage  imageNew  =  new  BufferedImage(width*2,height,BufferedImage.TYPE_INT_RGB);      
  274.             //BufferedImage  imageNew  =  new  BufferedImage(width+width2,height3,BufferedImage.TYPE_INT_RGB);       
  275.             imageNew.setRGB(0,0,width,height,imageArrayOne,0,width);//设置左半部分的RGB     
  276.             imageNew.setRGB(width,0,width,height,ImageArrayTwo,0,width);//设置右半部分的RGB    
  277.             //imageNew.setRGB(width,0,width2,height2,ImageArrayTwo,0,width2);//设置右半部分的RGB       
  278.              
  279.             File  outFile  =  new  File(toPath);      
  280.             ImageIO.write(imageNew,  imageFormat,  outFile);//写图片   
  281.         } catch (Exception e) {    
  282.             e.printStackTrace();    
  283.         }    
  284.     }  
  285.       
  286.     /** 
  287.      * 横向拼接一组(多张)图像 
  288.      * @param pics  将要拼接的图像 
  289.      * @param type 图像写入格式 
  290.      * @param dst_pic 图像写入路径 
  291.      * @return 
  292.      */  
  293.     public  boolean joinImageListHorizontal(String[] pics, String type, String dst_pic) {     
  294.         try {    
  295.             int len = pics.length;    
  296.             if (len < 1) {    
  297.                 System.out.println("pics len < 1");    
  298.                 return false;    
  299.             }    
  300.             File[] src = new File[len];    
  301.             BufferedImage[] images = new BufferedImage[len];    
  302.             int[][] imageArrays = new int[len][];    
  303.             for (int i = 0; i < len; i++) {    
  304.                 src[i] = new File(pics[i]);    
  305.                 images[i] = ImageIO.read(src[i]);    
  306.                 int width = images[i].getWidth();    
  307.                 int height = images[i].getHeight();    
  308.                 imageArrays[i] = new int[width * height];// 从图片中读取RGB       
  309.                 imageArrays[i] = images[i].getRGB(00, width, height,  imageArrays[i], 0, width);    
  310.             }    
  311.               
  312.             int dst_width = 0;    
  313.             int dst_height = images[0].getHeight();    
  314.             for (int i = 0; i < images.length; i++) {    
  315.                 dst_height = dst_height > images[i].getHeight() ? dst_height : images[i].getHeight();    
  316.                 dst_width += images[i].getWidth();  
  317.             }    
  318.             //System.out.println(dst_width);     
  319.             //System.out.println(dst_height);     
  320.             if (dst_height < 1) {    
  321.                 System.out.println("dst_height < 1");    
  322.                 return false;    
  323.             }   
  324.             /* 
  325.              * 生成新图片 
  326.              */     
  327.             BufferedImage ImageNew = new BufferedImage(dst_width, dst_height,  BufferedImage.TYPE_INT_RGB);    
  328.             int width_i = 0;  
  329.             for (int i = 0; i < images.length; i++) {    
  330.                 ImageNew.setRGB(width_i, 0, images[i].getWidth(), dst_height,  imageArrays[i], 0, images[i].getWidth());    
  331.                 width_i += images[i].getWidth();  
  332.             }    
  333.             File outFile = new File(dst_pic);    
  334.             ImageIO.write(ImageNew, type, outFile);// 写图片      
  335.         } catch (Exception e) {    
  336.             e.printStackTrace();    
  337.             return false;    
  338.         }    
  339.         return true;    
  340.     }  
  341.       
  342.     /** 
  343.      * 纵向拼接图片(两张) 
  344.      * @param firstSrcImagePath 读取的第一张图片 
  345.      * @param secondSrcImagePath    读取的第二张图片 
  346.      * @param imageFormat 图片写入格式 
  347.      * @param toPath    图片写入路径 
  348.      */  
  349.     public void joinImagesVertical(String firstSrcImagePath, String secondSrcImagePath,String imageFormat, String toPath){    
  350.         try {    
  351.             //读取第一张图片       
  352.             File  fileOne  =  new  File(firstSrcImagePath);      
  353.             BufferedImage  imageOne = ImageIO.read(fileOne);      
  354.             int  width  =  imageOne.getWidth();//图片宽度       
  355.             int  height  =  imageOne.getHeight();//图片高度       
  356.             //从图片中读取RGB       
  357.             int[]  imageArrayOne  =  new  int[width*height];      
  358.             imageArrayOne  =  imageOne.getRGB(0,0,width,height,imageArrayOne,0,width);      
  359.          
  360.             //对第二张图片做相同的处理       
  361.             File  fileTwo  =  new  File(secondSrcImagePath);      
  362.             BufferedImage  imageTwo  =  ImageIO.read(fileTwo);   
  363.             int width2 = imageTwo.getWidth();  
  364.             int height2 = imageTwo.getHeight();  
  365.             int[]   ImageArrayTwo  =  new  int[width2*height2];      
  366.             ImageArrayTwo  =  imageTwo.getRGB(0,0,width,height,ImageArrayTwo,0,width);      
  367.             //ImageArrayTwo  =  imageTwo.getRGB(0,0,width2,height2,ImageArrayTwo,0,width2);    
  368.          
  369.             //生成新图片   
  370.             //int width3 = (width>width2 || width==width2)?width:width2;   
  371.             BufferedImage  imageNew  =  new  BufferedImage(width,height*2,BufferedImage.TYPE_INT_RGB);      
  372.             //BufferedImage  imageNew  =  new  BufferedImage(width3,height+height2,BufferedImage.TYPE_INT_RGB);       
  373.             imageNew.setRGB(0,0,width,height,imageArrayOne,0,width);//设置上半部分的RGB       
  374.             imageNew.setRGB(0,height,width,height,ImageArrayTwo,0,width);//设置下半部分的RGB   
  375.             //imageNew.setRGB(0,height,width2,height2,ImageArrayTwo,0,width2);//设置下半部分的RGB       
  376.          
  377.             File  outFile  =  new  File(toPath);      
  378.             ImageIO.write(imageNew,  imageFormat,  outFile);//写图片   
  379.         } catch (Exception e) {    
  380.             e.printStackTrace();    
  381.         }    
  382.     }  
  383.       
  384.     /** 
  385.      * 纵向拼接一组(多张)图像 
  386.      * @param pics      将要拼接的图像数组 
  387.      * @param type  写入图像类型 
  388.      * @param dst_pic   写入图像路径 
  389.      * @return 
  390.      */  
  391.     public  boolean joinImageListVertical(String[] pics, String type, String dst_pic) {     
  392.         try {    
  393.             int len = pics.length;    
  394.             if (len < 1) {    
  395.                 System.out.println("pics len < 1");    
  396.                 return false;    
  397.             }    
  398.              File[] src = new File[len];    
  399.              BufferedImage[] images = new BufferedImage[len];    
  400.              int[][] imageArrays = new int[len][];    
  401.              for (int i = 0; i < len; i++) {    
  402.                 //System.out.println(i);   
  403.                 src[i] = new File(pics[i]);    
  404.                 images[i] = ImageIO.read(src[i]);    
  405.                 int width = images[i].getWidth();    
  406.                 int height = images[i].getHeight();    
  407.                 imageArrays[i] = new int[width * height];// 从图片中读取RGB      
  408.                 imageArrays[i] = images[i].getRGB(00, width, height,  imageArrays[i], 0, width);    
  409.             }    
  410.                
  411.             int dst_height = 0;    
  412.             int dst_width = images[0].getWidth();    
  413.             for (int i = 0; i < images.length; i++) {    
  414.                 dst_width = dst_width > images[i].getWidth() ? dst_width : images[i].getWidth();    
  415.                 dst_height += images[i].getHeight();    
  416.             }    
  417.             //System.out.println(dst_width);     
  418.             //System.out.println(dst_height);     
  419.             if (dst_height < 1) {    
  420.                 System.out.println("dst_height < 1");    
  421.                 return false;    
  422.             }    
  423.             /* 
  424.              * 生成新图片 
  425.              */     
  426.             BufferedImage ImageNew = new BufferedImage(dst_width, dst_height,  BufferedImage.TYPE_INT_RGB);    
  427.             int height_i = 0;    
  428.             for (int i = 0; i < images.length; i++) {    
  429.                 ImageNew.setRGB(0, height_i, dst_width, images[i].getHeight(),  imageArrays[i], 0, dst_width);    
  430.                 height_i += images[i].getHeight();    
  431.             }    
  432.             File outFile = new File(dst_pic);    
  433.             ImageIO.write(ImageNew, type, outFile);// 写图片      
  434.         } catch (Exception e) {    
  435.             e.printStackTrace();    
  436.             return false;    
  437.         }    
  438.         return true;    
  439.     }    
  440.       
  441.     /** 
  442.      * 合并图片(按指定初始x、y坐标将附加图片贴到底图之上) 
  443.      * @param negativeImagePath 背景图片路径 
  444.      * @param additionImagePath 附加图片�%A
原创粉丝点击