jpgtoxlsx.JPGtoXLSX.java Source code

Java tutorial

Introduction

Here is the source code for jpgtoxlsx.JPGtoXLSX.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jpgtoxlsx;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 *
 * @author Jasper
 */
public class JPGtoXLSX {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {

        XSSFWorkbook myExcel = new XSSFWorkbook();
        XSSFSheet sheet = myExcel.createSheet("Image");

        BufferedImage image = null;

        int width, height;
        int xlrows;
        //int pixel;

        File fimg;
        //open image
        try {
            fimg = new File("C:\\excel\\Test.jpg");
            image = ImageIO.read(fimg);
        } catch (IOException e) {
            System.out.println(e);
        }

        width = image.getWidth();
        //System.out.println(width);
        height = image.getHeight();
        //System.out.println(height);

        xlrows = height * 3;

        //System.out.println(pixel);

        int r, g, b;

        int w = image.getWidth();
        int h = image.getHeight();
        System.out.println("Width: " + w + "Height: " + h);
        System.out.println("Generating RGB values..");

        XSSFCellStyle style1 = myExcel.createCellStyle();
        XSSFCellStyle style2 = myExcel.createCellStyle();
        XSSFCellStyle style3 = myExcel.createCellStyle();

        for (int i = 1; i < ++h; i++) {

            if (i == image.getHeight()) {
                break;
            }

            XSSFRow row1 = sheet.createRow((i * 3 - 3));
            XSSFRow row2 = sheet.createRow((i * 3 - 2));
            XSSFRow row3 = sheet.createRow((i * 3 - 1));

            for (int j = 0; j < ++w; j++) {

                if (j == image.getWidth()) {
                    break;
                }

                //System.out.println("I: " + i);
                //System.out.println("J: " + j + "\n");
                int x = i;
                int y = j;

                //System.out.println("X: " + x + "Y: " + y + "\r");
                //System.out.println("Y: " + y);
                int pixel = image.getRGB(y, x);
                r = (pixel >> 16) & 0xff;
                g = (pixel >> 8) & 0xff;
                b = (pixel) & 0xff;

                XSSFCell cell1 = row1.createCell(y);
                XSSFCell cell2 = row2.createCell(y);
                XSSFCell cell3 = row3.createCell(y);

                cell1.setCellValue(Integer.toString(r));
                cell2.setCellValue(Integer.toString(g));
                cell3.setCellValue(Integer.toString(b));

                style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(r, 0, 0)));
                ;
                style1.setFillPattern(CellStyle.SOLID_FOREGROUND);

                style2.setFillForegroundColor(new XSSFColor(new java.awt.Color(0, g, 0)));
                style2.setFillPattern(CellStyle.SOLID_FOREGROUND);

                style3.setFillForegroundColor(new XSSFColor(new java.awt.Color(0, 0, b)));
                style3.setFillPattern(CellStyle.SOLID_FOREGROUND);

                cell1.setCellStyle(style1);
                cell2.setCellStyle(style2);
                cell3.setCellStyle(style3);

                //System.out.println("x,y: " + j + ", " + i);
                //System.out.println("R: " + r + " G: " + g + " B: " + b + "\n");
            }
        }
        System.out.println("RGB values extracted.");
        System.out.println("Generating image");

        myExcel.write(new FileOutputStream("excel.xlsx"));
        myExcel.close();

    }

}