Yapmaya çalıştığım şey, bir görüntüyü o görüntünün Excel temsiline dönüştüren bir program yazmaktır. Şu anda yaptığım şey, görüntüyü yüklediğim ve görüntü için RGB değerlerini bir 2B tamsayı dizisine alıyorum.Apache poi Stil ayarı bir süre sonra durur
Karşılaştığım sorun şu ki. Hücrelerim aniden hiç stil vermiyor! Arka plan rengine sahip bir çift hücreden sonra, geri kalanı beyaz bırakıldı, ben görüntüyü 60 * 60 çözünürlük olarak sınırladığımdan beri 4,0000 stil sınırını geçmeyeceğim. Yani yanlış yaptığımdan emin değilim.
Benim asıl sınıfı:
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();
}
}
benim imageHandler sınıfı:
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;
}
}
DÜZENLEME: yeni güncellenmiş kod
sürücü:
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;
}
}
benim görüntü işleyicisi sınıf temeldeaynı, ancak görüntüyü yeniden boyutlandırmıyorum.
Bu, ben renk, daha karmaşık bir şeyle ilgileniyorum ve benim "test.jpg"
İşte
excel kenara (rotasyon neye benzediğini bir ekran görüntüsü var olan sadece ne yapmam gerektiğini) çöpe tam olarak emin değiliz
döner
Paletteki yedek renkler tükeniyor olabilir mi? IIRC, Excel'de (.xls) tanımladığınız farklı renklerin sayısından çok daha düşük bir limit var. – Gagravarr
@Gagravarr Hmm kullanarak, hücre stilleri sayısından daha fazla emin değilim. Tamamen emin değilim. 4000 stilleri bir çalışma zamanı istisnası var. Eğer durum buysa, nasıl ilerlemem gerektiği konusunda herhangi bir öneriniz var mı? Varolan bir rengin üzerine yazmayı denedim ve bunu kullandım ama bu da işe yaramadı. :( –
[PaletteRecord için Dosya Biçimi Belirtimi] (http://msdn.microsoft.com/en-us/library/dd909801%28v=office.12%29.aspx), 56 renk sınırının zor olduğunu .xls' dosyası XSSF/.xlsx'e geçebilir misiniz?Bu, renkleri yapmanın farklı bir yoluna sahiptir, bu kısıtlama olmadan – Gagravarr