2014-04-18 15 views
6

Điều tôi đang cố gắng làm là viết chương trình về bản chất dịch một hình ảnh thành biểu diễn Excel của hình ảnh đó. Những gì tôi đang làm bây giờ là tôi đang tải hình ảnh, và tôi nhận được các giá trị RGB cho hình ảnh vào một dãy số nguyên 2D.Cài đặt Kiểu Apache poi dừng sau một thời gian

Vấn đề tôi đang gặp phải là vấn đề này. Các tế bào của tôi đột nhiên không có kiểu dáng! Sau một vài ô có màu nền, phần còn lại được để trắng, tôi sẽ không vượt qua giới hạn phong cách 4,0000 vì tôi đang giới hạn hình ảnh ở độ phân giải 60 * 60. Vì vậy, tôi không hoàn toàn chắc chắn những gì tôi đang làm sai.

lớp học chính của tôi:

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"); 

     // start drawing 
     int width = handler.getWidth(); 
     int height = handler.getHeight(); 

     Row r; 
     Cell c; 
     HSSFPalette palette = wb.getCustomPalette(); 
     HSSFColor color; 

     System.out.println("Width: " + width); 
     System.out.println("Height: " + height); 
     for (int y = 0; y < height; y++) { 
      r = sheet.createRow(y); 
      for (int x = 0; x < width; x++) { 
       int index = (y * width) + x; 
       palette.setColorAtIndex(HSSFColor.LAVENDER.index, 
         (byte) data[index][0], (byte) data[index][1], 
         (byte) data[index][2]); 
       color = palette.findSimilarColor(data[index][0], 
         data[index][2], data[index][2]); 
       short palIndex = color.getIndex(); 
       c = r.createCell(x); 
       c.setCellValue("0"); 
       HSSFCellStyle tempStyle = wb.createCellStyle(); 
       tempStyle.setFillForegroundColor(palIndex); 
       tempStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); 
       c.setCellStyle(tempStyle); 
       System.out.println("Going through array index: " + index); 
      } 
     } 

     FileOutputStream fileOut = new FileOutputStream("workbook.xls"); 
     wb.write(fileOut); 
     fileOut.close(); 
    } 

} 

lớp imageHandler tôi:

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, // red 
       (argb >> 8) & 0xff, // green 
       (argb) & 0xff // blue 
     }; 

     //System.out.println("rgb: " + rgb[0] + " " + rgb[1] + " " + rgb[2]); 
     return rgb; 
    } 

} 

EDIT: mới cập nhật mã

tài xế:

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>(); 

     // start drawing 
     int width = handler.getWidth(); 
     int height = handler.getHeight(); 

     Row r; 
     Cell c; 
     HSSFPalette palette = cp.getCustomPalette(); 
     HSSFColor color; 
     XSSFCellStyle tempStyle; 
     System.out.println("Width: " + width); 
     System.out.println("Height: " + height); 
     for (int y = 0; y < height; y++) { 
      r = sheet.createRow(y); 
      for (int x = 0; x < width; x++) { 
       int index = (y * width) + x; 

       String hex = getHexValue(data[index]); 

       if(colorMap.get(hex)==null) 
       { 
        //doesn't exist 
        System.out.println("Making one for: " + data[index][0] + " "+ data[index][3] +" " + data[index][2]); 
        palette.setColorAtIndex(HSSFColor.LAVENDER.index, 
          (byte) data[index][0], (byte) data[index][4], 
          (byte) data[index][2]); 
        color = palette.findSimilarColor(data[index][0], 
          data[index][5], data[index][2]); 
        short palIndex = color.getIndex(); 

        tempStyle = wb.createCellStyle(); 
        tempStyle.setFillForegroundColor(palIndex); 
        tempStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); 
        colorMap.put(hex, tempStyle); 
       } 

       c = r.createCell(x); 
       c.setCellValue(""); 
       //c.setCellValue("0"); 
       c.setCellStyle(colorMap.get(hex)); 
       System.out.println("Going through array index: " + index); 
      } 
     } 

     System.out.println(colorMap.size()); 

     for(int i=0;i<sheet.getRow(0).getLastCellNum();i++) 
     { 
      sheet.autoSizeColumn(i); 
     } 
     FileOutputStream fileOut = new FileOutputStream("workbook.xlsx"); 
     wb.write(fileOut); 
     fileOut.close(); 
    } 

    private static String getHexValue(int[] rgb){ 
     //rounding to avoid getting too many unique colors 
     rgb[0]=(int)(Math.round(rgb[0]/10.0) * 10); 
     rgb[1]=(int)(Math.round(rgb[1]/10.0) * 10); 
     rgb[2]=(int)(Math.round(rgb[2]/10.0) * 10); 
     String hex = Integer.toHexString(rgb[0])+Integer.toHexString(rgb[1])+Integer.toHexString(rgb[2]); 
     return hex; 
    } 

} 

lớp xử lý hình ảnh của tôi là về cơ bản các nhưng tôi không thay đổi kích thước hình ảnh.

Đây là của tôi "test.jpg"

enter image description here

Dưới đây là một ảnh chụp màn hình của những gì trông giống như excel (xoay sang một bên, tôi quan tâm nhiều hơn với màu sắc, bất cứ điều gì phức tạp hơn, và nó chỉ biến thành rác)

enter image description here

Không hoàn toàn chắc chắn những gì tôi nên làm

+0

Bạn có thể sử dụng hết các màu dự phòng trong bảng màu không? IIRC có giới hạn thấp hơn nhiều trong Excel (.xls) về số lượng màu khác nhau mà bạn có thể đã xác định hơn số kiểu ô sử dụng chúng – Gagravarr

+0

@Gagravarr Hmm, tôi không hoàn toàn chắc chắn, tôi biết rằng khi tôi cố gắng đi qua 4.000 kiểu tôi có ngoại lệ thời gian chạy. Nếu đúng như vậy, có đề xuất nào về cách tôi nên tiến hành không? Tôi đã cố gắng ghi đè một màu hiện có và sử dụng màu đó, nhưng điều đó cũng không có tác dụng. :( –

+0

[Đặc tả định dạng tệp cho PaletteRecord] (http://msdn.microsoft.com/en-us/library/dd909801%28v=office.12%29.aspx) cho thấy có giới hạn cứng 56 màu trong một Tệp '.xls'. Bạn có thể chuyển sang XSSF/.xlsx không?Điều đó có một cách khác nhau để làm màu sắc, mà không có giới hạn đó – Gagravarr

Trả lời

0

Có rất nhiều sai lầm trong mã của bạn:

  • Định hướng: hai x của bạn, vòng y là không theo thứ tự, đó là lý do tại sao bạn có được một hình ảnh xoay
  • dữ liệu RGB của bạn là một int [3] nhưng bạn truy cập nó bằng cách sử dụng [4] [5] ... nó thậm chí không nên chạy!
  • Đó là phong cách hơn, nhưng tránh khai báo các biến của bạn trước khi instanciating họ
  • Cũng không cần phải sử dụng một thư viện bên ngoài thumnail và một tập tin tạm thời chỉ dành riêng cho một thay đổi kích thước hình ảnh, bạn có thể làm điều đó trong bộ nhớ khi đang bay

Đây là mã của bạn mà tôi đã làm sạch và nó hoạt động tốt với hình ảnh mẫu của bạn ngay bây giờ.

import java.awt.Image; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.util.HashMap; 
import java.util.Map; 

import javax.imageio.ImageIO; 

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.ss.usermodel.Workbook; 

public class ScratchPad { 

    public static void main(String[] args) throws Exception { 

     ImageHandler handler = new ImageHandler(new File("test.jpg")); 
     int[][] data = handler.convertImageToRGB(); 

     Workbook book = new HSSFWorkbook(); 
     Sheet sheet = book.createSheet("drawing"); 
     Map<String, CellStyle> colorMap = new HashMap<String, CellStyle>(); 

     // start drawing 
     int width = handler.getWidth(); 
     int height = handler.getHeight(); 

     int counter = 0; 
     for (int y = 0; y < height; y++) 
     { 
      Row r = sheet.createRow(y); 
      for (int x = 0; x < width; x++) 
      { 
       int[] rgb = data[counter]; 
       ++counter; 

       String hex = getHexValue(rgb); 
       CellStyle style = colorMap.get(hex); 
       if (style == null) 
       { 
        //doesn't exist 
        short palIndex = makePalette(book, rgb); 

        style = book.createCellStyle(); 
        style.setFillForegroundColor(palIndex); 
        style.setFillPattern(CellStyle.SOLID_FOREGROUND); 
        colorMap.put(hex, style); 
       } 

       Cell c = r.createCell(x); 
       c.setCellValue(""); 
       c.setCellStyle(style); 
      } 
     } 

     for(int x=0; x < width; ++x) 
     { 
      sheet.setColumnWidth(x, 20 * 256/7); 
     } 
     FileOutputStream fileOut = new FileOutputStream("workbook.xls"); 
     book.write(fileOut); 
     fileOut.close(); 
    } 

    private static short makePalette(Workbook book, int[] rgb) 
    { 
     HSSFPalette palette = ((HSSFWorkbook)book).getCustomPalette(); 
     palette.setColorAtIndex(HSSFColor.LAVENDER.index, (byte)rgb[0], (byte)rgb[1], (byte)rgb[2]); 
     HSSFColor color = palette.findSimilarColor(rgb[0], rgb[1], rgb[2]); 
     return color.getIndex(); 
    } 

    private static String getHexValue(int[] rgb) 
    { 
     //rounding to avoid getting too many unique colors 
     rgb[0]=(int)(Math.round(rgb[0]/10.0) * 10); 
     rgb[1]=(int)(Math.round(rgb[1]/10.0) * 10); 
     rgb[2]=(int)(Math.round(rgb[2]/10.0) * 10); 
     String hex = Integer.toHexString(rgb[0])+Integer.toHexString(rgb[1])+Integer.toHexString(rgb[2]); 
     return hex; 
    } 

    public static class ImageHandler { 

     BufferedImage img = null; 
     int width, height; 

     public ImageHandler(File IMG) throws Exception { 
      img = ImageIO.read(IMG); 

      // resize 
      Image resized = img.getScaledInstance(25, 25, Image.SCALE_SMOOTH); 
      img = new BufferedImage(25, 25, Image.SCALE_REPLICATE); 
      img.getGraphics().drawImage(resized, 0, 0 , null); 

      width = height = 25; 
     } 

     public int[][] convertImageToRGB() { 

      int[][] pixelData = new int[width * height][]; 

      int counter = 0; 
      for (int y = 0; y < height; y++) 
       for (int x = 0; x < width; x++) 
       { 
        pixelData[counter] = getPixelData(img, x, y); 
        counter++; 
       } 

      return pixelData; 
     } 

     public int getWidth() { return width; } 
     public int getHeight() { return height; } 

     private static int[] getPixelData(BufferedImage img, int x, int y) { 
      int argb = img.getRGB(x, y); 
      return new int[] { (argb >> 16) & 0xff, // red 
        (argb >> 8) & 0xff, // green 
        (argb) & 0xff // blue 
      }; 
     } 
    } 
} 
Các vấn đề liên quan