java實(shí)現(xiàn)圖片用Excel畫(huà)出來(lái)
本文實(shí)例為大家分享了java用Excel將圖片畫(huà)出來(lái)的具體代碼,供大家參考,具體內(nèi)容如下
能夠?qū)⑷魏螆D片在excel上利用單元格背景完整的描繪出來(lái)。
像網(wǎng)絡(luò)上出現(xiàn)的用excel畫(huà)出超級(jí)瑪麗等等,各種圖片都能在excel上"畫(huà)"出來(lái)。
圖片我沒(méi)有經(jīng)過(guò)特殊處理,所以轉(zhuǎn)換的圖片不能太大,有多大的圖片就要有多少的單元格。如640*480就有307200的單元格。
如要轉(zhuǎn)換的圖片:

轉(zhuǎn)換后在excel中的效果:

沒(méi)多大意義練練手:
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);
}
}
//寫(xiě)入Excel對(duì)象
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("轉(zhuǎn)換開(kāi)始");
//轉(zhuǎn)換執(zhí)行的方法參數(shù) args[0]輸入的圖片路徑 args[1]輸出的excel路徑
// helper.exec( "mslogo.jpg","1.xls");
helper.exec(args[0],args[1]);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java 輸入一個(gè)數(shù)字組成的數(shù)組(輸出該數(shù)組的最大值和最小值)
這篇文章主要介紹了java 輸入一個(gè)數(shù)字組成的數(shù)組,輸出該數(shù)組的最大值和最小值,需要的朋友可以參考下2017-02-02
Java+ElasticSearch+Pytorch實(shí)現(xiàn)以圖搜圖功能
這篇文章主要為大家詳細(xì)介紹了Java如何利用ElasticSearch和Pytorch實(shí)現(xiàn)以圖搜圖功能,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-06-06
spring中在xml配置中加載properties文件的步驟
這篇文章主要介紹了在spring中如何在xml配置中加載properties文件,本文分步驟給大家介紹在XML配置中加載properties文件的方法,需要的朋友可以參考下2023-07-07
Java進(jìn)階之FileUpload完成上傳的實(shí)例
這篇文章主要介紹了 Java進(jìn)階之FileUpload完成上傳的實(shí)例的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09
springboot項(xiàng)目配置多個(gè)kafka的示例代碼
這篇文章主要介紹了springboot項(xiàng)目配置多個(gè)kafka,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
SpringBoot結(jié)合FreeMarker視圖渲染的實(shí)現(xiàn)
FreeMarker它允許開(kāi)發(fā)人員使用模板和數(shù)據(jù)來(lái)生成輸出文本,如HTML網(wǎng)頁(yè)、電子郵件、配置文件和源代碼等,本文主要介紹了SpringBoot結(jié)合FreeMarker視圖渲染的實(shí)現(xiàn),感興趣的可以了解一下2024-03-03

