java实现图片用Excel画出来

所属分类: 软件编程 / java 阅读数: 65
收藏 0 赞 0 分享

本文实例为大家分享了java用Excel将图片画出来的具体代码,供大家参考,具体内容如下

能够将任何图片在excel上利用单元格背景完整的描绘出来。

像网络上出现的用excel画出超级玛丽等等,各种图片都能在excel上"画"出来。

图片我没有经过特殊处理,所以转换的图片不能太大,有多大的图片就要有多少的单元格。如640*480就有307200的单元格。

如要转换的图片:

转换后在excel中的效果:

没多大意义练练手:

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
 
import javax.swing.ImageIcon;
 
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.Colour;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
 
public class Helper {
 private BufferedImage getBufferedImage(String filepath)
 {
 ImageIcon imgicon=new ImageIcon(filepath);
 BufferedImage bufferedImage = new BufferedImage(imgicon.getIconWidth(),imgicon.getIconHeight(),BufferedImage.TYPE_INT_RGB);  
 
 bufferedImage.createGraphics().drawImage(imgicon.getImage(), 0, 0,null);
 return bufferedImage;
 
 }
 
 private Colour getNearestColour(Color awtColor) { 
  Colour color = null; 
  Colour[] colors = Colour.getAllColours(); 
  if ((colors != null) && (colors.length > 0)) { 
  Colour crtColor = null; 
  int[] rgb = null; 
  int diff = 0; 
  int minDiff = 999; 
  
  for (int i = 0; i < colors.length; i++) { 
  crtColor = colors[i]; 
  rgb = new int[3]; 
  rgb[0] = crtColor.getDefaultRGB().getRed(); 
  rgb[1] = crtColor.getDefaultRGB().getGreen(); 
  rgb[2] = crtColor.getDefaultRGB().getBlue(); 
  
  diff = Math.abs(rgb[0] - awtColor.getRed()) 
   + Math.abs(rgb[1] - awtColor.getGreen()) 
   + Math.abs(rgb[2] - awtColor.getBlue()); 
  
  if (diff < minDiff) { 
   minDiff = diff; 
   color = crtColor; 
  } 
  } 
  } 
  if (color == null) 
  color = Colour.BLACK; 
  return color; 
  } 
 public void exec(String convertFromImage,String createxls) throws Exception
 {
 
 WorkbookSettings ws = new WorkbookSettings();
  ws.setLocale(new Locale("en", "EN"));
  WritableWorkbook workbook = Workbook.createWorkbook(new File(createxls), ws);
  WritableSheet s2 = workbook.createSheet("picture", 0);
  BufferedImage buffimage= getBufferedImage(convertFromImage);
  int width=buffimage.getWidth();
  int heigh=buffimage.getHeight();
  for(int i=0;i<width;i++)
  {
   for(int h=0;h<heigh;h++)
  {
    WritableCellFormat greyBackground = new WritableCellFormat();
    Color c = new Color(buffimage.getRGB(i, h)); 
   
  
  greyBackground.setBackground( getNearestColour(c) );
  
  Label lr = new Label(i, h, "", greyBackground);
 
  s2.addCell(lr);
  }
  
  }
   
 
  //写入Excel对象 
  workbook.write(); 
 
 
  workbook.close();
 
 }
 /**
 * @param args
 * @throws IOException 
 * @throws BiffException 
 */
 public static void main(String[] args) throws Exception {
 Helper helper=new Helper();
 
 System.out.println("输入的图片:"+args[0]);
 System.out.println("输出的excel:"+args[1]);
 System.out.println("转换开始");
 //转换执行的方法参数 args[0]输入的图片路径 args[1]输出的excel路径
// helper.exec( "mslogo.jpg","1.xls");
 helper.exec(args[0],args[1]);
 }
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Java基于反射机制实现全部注解获取的方法示例

这篇文章主要介绍了Java基于反射机制实现全部注解获取的方法,结合实例形式分析了java反射机制获取注解的具体实现方法与操作注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

Java 信号量Semaphore的实现

这篇文章主要介绍了Java 信号量Semaphore的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

eclipse+maven+spring mvc项目基本搭建过程

这篇文章主要介绍了eclipse+maven+spring mvc项目基本搭建过程,本文图文并茂给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Spring boot集成swagger2生成接口文档的全过程

这篇文章主要给大家介绍了关于Spring boot集成swagger2生成接口文档的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Java冒泡排序法和选择排序法的实现

这篇文章主要介绍了Java冒泡排序法和选择排序法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Cloud Alibaba教程之Sentinel的使用

这篇文章主要介绍了Spring Cloud Alibaba教程之Sentinel的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Josephus环的四种解法(约瑟夫环)基于java详解

这篇文章主要介绍了Josephus环的四种解法(约瑟夫环)基于java详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Java继承Thread类创建线程类示例

这篇文章主要介绍了Java继承Thread类创建线程类,结合实例形式分析了java线程操作相关使用技巧与注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

Java使用Callable和Future创建线程操作示例

这篇文章主要介绍了Java使用Callable和Future创建线程操作,结合实例形式分析了java使用Callable接口和Future类创建线程的相关操作技巧与注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

springBoot使用JdbcTemplate代码实例

这篇文章主要介绍了springBoot使用JdbcTemplate代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多