What I'm trying to do is write a program that essentially translates the image into an Excel view of that image itself. What I am doing now is that I load the image and I get the RGB values ββfor the image in a 2D array of integers.
The problem I am facing is this. My cells suddenly have no style! After a couple of cells with a background color, the rest remains white, I do not pass the limit of 40,000 styles, as I limit the image to 60 * 60 resolution. Therefore, I'm not quite sure what I'm doing wrong.
My main class:
package excelArtist; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFPalette; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; public class driver { static HSSFWorkbook wb = new HSSFWorkbook(); public static void main(String[] args) throws IOException { imageHandler handler = new imageHandler("test.jpg"); int[][] data = handler.convertImageToRGB(); Sheet sheet = wb.createSheet("drawing");
my imageHandler class:
package excelArtist; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import net.coobird.thumbnailator.Thumbnails; public class imageHandler { BufferedImage img = null; public imageHandler(String IMG) { try { Thumbnails.of(new File(IMG)) .size(25, 25) .toFile(new File("resized"+IMG)); img = ImageIO.read(new File("resized"+IMG)); } catch (IOException e) { e.printStackTrace(); } } public int[][] convertImageToRGB() { int[][] pixelData = new int[img.getHeight() * img.getWidth()][3]; int[] rgb; int counter = 0; for (int i = 0; i < img.getWidth(); i++) { for (int j = 0; j < img.getHeight(); j++) { rgb = getPixelData(img, i, j); for (int k = 0; k < rgb.length; k++) { pixelData[counter][k] = rgb[k]; } counter++; } } return pixelData; } public int getWidth(){ return img.getWidth(); } public int getHeight(){ return img.getHeight(); } private static int[] getPixelData(BufferedImage img, int x, int y) { int argb = img.getRGB(x, y); int rgb[] = new int[] { (argb >> 16) & 0xff,
EDIT: Updated Code
driver:
package excelArtist; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFPalette; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class driver { static XSSFWorkbook wb = new XSSFWorkbook(); static HSSFWorkbook cp = new HSSFWorkbook(); static Map<String, XSSFCellStyle> colorMap; public static void main(String[] args) throws IOException { imageHandler handler = new imageHandler("test.jpg"); int[][] data = handler.convertImageToRGB(); Sheet sheet = wb.createSheet("drawing"); colorMap = new HashMap<String, XSSFCellStyle>();
my image handler class is essentially the same, but I am not resizing the image.
This is my "test.jpg"

Here is a screenshot of how Excel looks (rotation to the side, I'm more interested in color, something more complex, and it just turns into garbage)

Not quite sure what I should do