JAVA对图片操作(二)

简介: JAVA对图片操作

 /**

    * 将一组图片一次性附加合并到底图上

    * @param negativeImagePath 源图像(底图)路径

    * @param additionImageList 附加图像信息列表

    * @param imageFormat 图像写入格式

    * @param toPath 图像写入路径

    * @throws IOException

    */

   public void mergeImageList(String negativeImagePath,List additionImageList,String imageFormat, String toPath) throws IOException{

   InputStream is= null;

   InputStream is2= null;

   OutputStream os = null;

   try{

   is=new FileInputStream(negativeImagePath);

   BufferedImage image=ImageIO.read(is);

   //Graphics g=image.getGraphics();

   Graphics2D g = image.createGraphics();;

   BufferedImage image2 = null;

   if(additionImageList != null){

   for(int i=0;i<additionImageList.size();i++){

   //解析附加图片信息:x坐标、 y坐标、 additionImagePath附加图片路径

   //图片信息存储在一个数组中

   String[] additionImageInfo = (String[]) additionImageList.get(i);

   int x = Integer.parseInt(additionImageInfo[0]);

   int y = Integer.parseInt(additionImageInfo[1]);

   String additionImagePath = additionImageInfo[2];

   //读取文件输入流,并合并图片

   is2 = new FileInputStream(additionImagePath);

   //System.out.println(x+"  :  "+y+"  :  "+additionImagePath);

   image2 = ImageIO.read(is2);

              g.drawImage(image2,x,y,null);

   }

   }

           os = new FileOutputStream(toPath);

           ImageIO.write(image,  imageFormat,  os);//写图片

           //JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);

           //enc.encode(image);

   }catch(Exception e){

   e.printStackTrace();

   }finally{

   if(os != null){

   os.close();

   }

   if(is2 != null){

   is2.close();

   }

   if(is != null){

   is.close();

   }

   }

   }

 

   /**

    * 将附加图片合并到底图的左上角

    * @param negativeImagePath 底图路径

    * @param additionImagePath 附加图片路径

    * @param toPath 合成图片写入路径

    * @throws IOException

    */

   public void mergeBothImageTopleftcorner(String negativeImagePath,String additionImagePath,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.drawImage(image2,0,0,null);

           os = new FileOutputStream(toPath);

           JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);

           enc.encode(image);

   }catch(Exception e){

   e.printStackTrace();

   }finally{

   if(os != null){

   os.close();

   }

   if(is2 != null){

   is2.close();

   }

   if(is != null){

   is.close();

   }

   }

   }

 

   /**

    * 将附加图片合并到底图的右上角

    * @param negativeImagePath 底图路径

    * @param additionImagePath 附加图片路径

    * @param toPath 合成图片写入路径

    * @throws IOException

    */

   public void mergeBothImageToprightcorner(String negativeImagePath,String additionImagePath,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.drawImage(image2,image.getWidth()-image2.getWidth(),0,null);

           os = new FileOutputStream(toPath);

           JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);

           enc.encode(image);

   }catch(Exception e){

   e.printStackTrace();

   }finally{

   if(os != null){

   os.close();

   }

   if(is2 != null){

   is2.close();

   }

   if(is != null){

   is.close();

   }

   }

   }

 

   /**

    * 将附加图片合并到底图的左下角

    * @param negativeImagePath 底图路径

    * @param additionImagePath 附加图片路径

    * @param toPath 合成图片写入路径

    * @throws IOException

    */

   public void mergeBothImageLeftbottom(String negativeImagePath,String additionImagePath,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.drawImage(image2,0,image.getHeight()-image2.getHeight(),null);

           os = new FileOutputStream(toPath);

           JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);

           enc.encode(image);

   }catch(Exception e){

   e.printStackTrace();

   }finally{

   if(os != null){

   os.close();

   }

   if(is2 != null){

   is2.close();

   }

   if(is != null){

   is.close();

   }

   }

   }


/**

    * 将附加图片合并到底图的左下角

    * @param negativeImagePath 底图路径

    * @param additionImagePath 附加图片路径

    * @param toPath 合成图片写入路径

    * @throws IOException

    */

   public void mergeBothImageRightbottom(String negativeImagePath,String additionImagePath,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.drawImage(image2,image.getWidth()-image2.getWidth(),image.getHeight()-image2.getHeight(),null);

           os = new FileOutputStream(toPath);

           JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);

           enc.encode(image);

   }catch(Exception e){

   e.printStackTrace();

   }finally{

   if(os != null){

   os.close();

   }

   if(is2 != null){

   is2.close();

   }

   if(is != null){

   is.close();

   }

   }

   }

 

   /**

    * 将附加图片合并到底图的正中央

    * @param negativeImagePath 底图路径

    * @param additionImagePath 附加图片路径

    * @param toPath 合成图片写入路径

    * @throws IOException

    */

   public void mergeBothImageCenter(String negativeImagePath,String additionImagePath,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.drawImage(image2,image.getWidth()/2-image2.getWidth()/2,image.getHeight()/2-image2.getHeight()/2,null);

           os = new FileOutputStream(toPath);

           JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);

           enc.encode(image);

   }catch(Exception e){

   e.printStackTrace();

   }finally{

   if(os != null){

   os.close();

   }

   if(is2 != null){

   is2.close();

   }

   if(is != null){

   is.close();

   }

   }

   }

 

   /**

    * 将附加图片合并到底图的上边中央

    * @param negativeImagePath 底图路径

    * @param additionImagePath 附加图片路径

    * @param toPath 合成图片写入路径

    * @throws IOException

    */

   public void mergeBothImageTopcenter(String negativeImagePath,String additionImagePath,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.drawImage(image2,image.getWidth()/2-image2.getWidth()/2,0,null);

           os = new FileOutputStream(toPath);

           JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);

           enc.encode(image);

   }catch(Exception e){

   e.printStackTrace();

   }finally{

   if(os != null){

   os.close();

   }

   if(is2 != null){

   is2.close();

   }

   if(is != null){

   is.close();

   }

   }

   }

 

   /**

    * 将附加图片合并到底图的下边中央

    * @param negativeImagePath 底图路径

    * @param additionImagePath 附加图片路径

    * @param toPath 合成图片写入路径

    * @throws IOException

    */

   public void mergeBothImageBottomcenter(String negativeImagePath,String additionImagePath,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.drawImage(image2,image.getWidth()/2-image2.getWidth()/2,image.getHeight()-image2.getHeight(),null);

           os = new FileOutputStream(toPath);

           JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);

           enc.encode(image);

   }catch(Exception e){

   e.printStackTrace();

   }finally{

   if(os != null){

   os.close();

   }

   if(is2 != null){

   is2.close();

   }

   if(is != null){

   is.close();

   }

   }

   }

 

   /**

    * 将附加图片合并到底图的左边中央

    * @param negativeImagePath 底图路径

    * @param additionImagePath 附加图片路径

    * @param toPath 合成图片写入路径

    * @throws IOException

    */

   public void mergeBothImageLeftcenter(String negativeImagePath,String additionImagePath,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.drawImage(image2,0,image.getHeight()/2-image2.getHeight()/2,null);

           os = new FileOutputStream(toPath);

           JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);

           enc.encode(image);

   }catch(Exception e){

   e.printStackTrace();

   }finally{

   if(os != null){

   os.close();

   }

   if(is2 != null){

   is2.close();

   }

   if(is != null){

   is.close();

   }

   }

   }


 /**

    * 将附加图片合并到底图的右边中央

    * @param negativeImagePath 底图路径

    * @param additionImagePath 附加图片路径

    * @param toPath 合成图片写入路径

    * @throws IOException

    */

   public void mergeBothImageRightcenter(String negativeImagePath,String additionImagePath,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.drawImage(image2,image.getWidth()-image2.getWidth(),image.getHeight()/2-image2.getHeight()/2,null);

           os = new FileOutputStream(toPath);

           JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);

           enc.encode(image);

   }catch(Exception e){

   e.printStackTrace();

   }finally{

   if(os != null){

   os.close();

   }

   if(is2 != null){

   is2.close();

   }

   if(is != null){

   is.close();

   }

   }

   }

 

   /**

    * 图片灰化操作

    * @param srcImage 读取图片路径

    * @param toPath 写入灰化后的图片路径

    * @param imageFormat 图片写入格式

    */

   public void grayImage(String srcImage,String toPath,String imageFormat){

   try{

   BufferedImage src = ImageIO.read(new File(srcImage));

       ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);

       ColorConvertOp op = new ColorConvertOp(cs, null);

       src = op.filter(src, null);

       ImageIO.write(src, imageFormat, new File(toPath));

   }catch(Exception e){

   e.printStackTrace();

   }

   }

相关文章
|
Java
Java开发实现图片URL地址检验,如何编码?
【10月更文挑战第14天】Java开发实现图片URL地址检验,如何编码?
790 4
|
Java
Java开发实现图片地址检验,如果无法找到资源则使用默认图片,如何编码?
【10月更文挑战第14天】Java开发实现图片地址检验,如果无法找到资源则使用默认图片,如何编码?
518 2
|
前端开发 JavaScript Java
Java中将图片转换为base64格式的技巧
这样,你就可以在Java中将图片转换为Base64格式了。这个方法的实现非常简单,只需要使用Java的内置库,无需任何额外的库。希望这个方法对你有所帮助。
1110 22
|
算法 Java Linux
java制作海报二:java使用Graphics2D 在图片上合成另一个照片,并将照片切割成头像,头像切割成圆形方法详解
这篇文章介绍了如何使用Java的Graphics2D类在图片上合成另一个照片,并将照片切割成圆形头像的方法。
473 1
java制作海报二:java使用Graphics2D 在图片上合成另一个照片,并将照片切割成头像,头像切割成圆形方法详解
Java系列之 重命名文件/图片,renameTo() 方法的使用
这篇文章介绍了Java中File类的renameTo()方法,包括其使用方式、参数、返回值以及一些使用时的注意事项,并通过示例代码演示了如何使用该方法来重命名文件。
Java系列之 重命名文件/图片,renameTo() 方法的使用
|
Java 数据安全/隐私保护
Java ffmpeg 实现视频加文字/图片水印功能
【10月更文挑战第22天】在 Java 中使用 FFmpeg 实现视频加文字或图片水印功能,需先安装 FFmpeg 并添加依赖(如 JavaCV)。通过构建 FFmpeg 命令行参数,使用 `drawtext` 滤镜添加文字水印,或使用 `overlay` 滤镜添加图片水印。示例代码展示了如何使用 JavaCV 实现文字水印。
1959 1
|
前端开发 小程序 Java
java基础:map遍历使用;java使用 Patten 和Matches 进行正则匹配;后端传到前端展示图片三种情况,并保存到手机
这篇文章介绍了Java中Map的遍历方法、使用Pattern和matches进行正则表达式匹配,以及后端向前端传输图片并保存到手机的三种情况。
1066 1
|
算法 Java Linux
java制作海报四:java BufferedImage 转 InputStream 上传至OSS。png 图片合成到模板(另一个图片)上时,透明部分变成了黑色
这篇文章主要介绍了如何将Java中的BufferedImage对象转换为InputStream以上传至OSS,并解决了png图片合成时透明部分变黑的问题。
1262 1
|
算法 搜索推荐 Java
java 后端 使用 Graphics2D 制作海报,画echarts图,带工具类,各种细节:如头像切割成圆形,文字换行算法(完美实验success),解决画上文字、图片后不清晰问题
这篇文章介绍了如何使用Java后端技术,结合Graphics2D和Echarts等工具,生成包含个性化信息和图表的海报,并提供了详细的代码实现和GitHub项目链接。
1315 0
java 后端 使用 Graphics2D 制作海报,画echarts图,带工具类,各种细节:如头像切割成圆形,文字换行算法(完美实验success),解决画上文字、图片后不清晰问题
|
存储 缓存 监控
Java——图片文件位于 bin 目录下,下载新图片会导致应用程序重启
【9月更文挑战第22天】在Java应用中,若图片位于bin目录下且下载新图片导致应用重启,可能是因为部署方式不当或资源监控机制过于敏感。解决方法包括:更改图片存储位置至独立目录;配置应用服务器减少资源监控敏感度;使用独立资源服务器托管静态资源;优化代码减少资源重复加载。具体方案需根据应用实际情况和技术栈调整。
370 4