1
我試圖製作一個讀取圖像的程序。並且在繪出之後,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);
}
}