2013-04-23 4 views
1

이미지를 읽는 programm를 만들려고했습니다. 그리고 추첨 후 MS의 이미지가 뛰어나지 만 모든 셀은 here과 같은 단일 픽셀입니다. 나는 멀리 조용하다고 생각하지만 셀을 채색하지 않으면 문제를 볼 수 없습니다. 누군가 나를 도울 수 있습니까?세포가 POI로 색상을 얻지 못합니다

원하는 경우 자유롭게 사용할 수있는 코드는 다음과 같습니다.

public class Engine { 


    ArrayList<Color> arr = new ArrayList<Color>(); 
    private int xx; 
    private int yy; 
    FileOutputStream out; 
    HSSFSheet sheet; 
    HSSFWorkbook wb; 


    public void process() throws AWTException, IOException{ 

     wb = new HSSFWorkbook(); 
     sheet = wb.createSheet(); 
     wb.setActiveSheet(0); 
     BufferedImage img = null; 
     try { 
      img = ImageIO.read(new File("res/images.jpg")); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      System.out.println("img file not found"); 
     } 


        for(int x=0;x<img.getWidth();x++){ 
         xx++; 
        for(int y=0;y<img.getHeight();y++){ 
         yy++; 
        int rgb = img.getRGB(x, y); 
        Color c = new Color(rgb); 
        printPixelARGB(rgb); 
        arr.add(c); 

        System.out.println("x: "+ x + " y:" + y +" color: " + c); 


       }} 
        out = new FileOutputStream("pic.xls"); 
        wb.write(out); 
        out.close(); 
       } 

      public void printPixelARGB(int pixel) { 
       int alpha = (pixel >> 24) & 0xff; 
       int red = (pixel >> 16) & 0xff; 
       int green = (pixel >> 8) & 0xff; 
       int blue = (pixel) & 0xff; 

        HSSFPalette palette = wb.getCustomPalette(); 
         HSSFCellStyle style = wb.createCellStyle(); 
         HSSFRow row = sheet.createRow((short) yy); 
         HSSFCell cell = row.createCell((short) xx); 
         cell.setCellValue(yy); 
         style.setFillForegroundColor(HSSFColor.LIME.index); 
         style.setFillBackgroundColor(HSSFColor.LIME.index); 
         palette.setColorAtIndex(HSSFColor.LIME.index, (byte) red, (byte) green, (byte) blue); 
         cell.setCellStyle(style); 

       } 

} 
나는 내 자신에 의해 그것을 알아낼 수

답변

0

솔루션은 다음과 같습니다

public class Engine { 


    ArrayList<Color> arr = new ArrayList<Color>(); 
    private int xx; 
    private int yy; 
    FileOutputStream out; 
    HSSFSheet sheet; 
    HSSFWorkbook wb; 
    Row r; 

    public void process() throws AWTException, IOException{ 

     wb = new HSSFWorkbook(); 
     sheet = wb.createSheet(); 
     wb.setActiveSheet(0); 
     BufferedImage img = null; 
     try { 
      img = ImageIO.read(new File("res/images.jpg")); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      System.out.println("img file not found"); 
     } 


        for(int x=0;x<img.getWidth();x++){ 
        xx++; 
        yy=0; 
         r = sheet.createRow(xx); 
        for(int y=0;y<img.getHeight();y++){ 
        yy++; 
        int rgb = img.getRGB(x, y); 
        Color c = new Color(rgb); 
        printPixelARGB(rgb); 
        arr.add(c); 

        System.out.println("x: "+ x + " y:" + y +" color: " + c); 


       }} 
        out = new FileOutputStream("pic.xls"); 
        wb.write(out); 
        out.close(); 
       } 

      public void printPixelARGB(int pixel) { 
       int alpha = (pixel >> 24) & 0xff; 
       int red = (pixel >> 16) & 0xff; 
       int green = (pixel >> 8) & 0xff; 
       int blue = (pixel) & 0xff; 

        Cell c = r.createCell(yy); 
        HSSFCellStyle style = wb.createCellStyle(); 
        HSSFColor col = setColor(wb, (byte)red, (byte)green,(byte)blue); 
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 
        style.setFillForegroundColor(col.getIndex()); 
        c.setCellStyle(style); 

       } 

      public HSSFColor setColor(HSSFWorkbook workbook, byte r,byte g, byte b){ 
       HSSFPalette palette = workbook.getCustomPalette(); 
       HSSFColor hssfColor = null; 
       try { 
       hssfColor= palette.findSimilarColor(r, g, b); 
       if (hssfColor == null){ 
        palette.setColorAtIndex(HSSFColor.LAVENDER.index, r, g,b); 
        hssfColor = palette.getColor(HSSFColor.LAVENDER.index); 
       } 
        } catch (Exception e) { 
       System.out.println("error"); 
       } 

        return hssfColor; 
       } 

} 
관련 문제