मुझे क्या करना कोशिश कर रहा हूँ एक प्रोग्राम है जो अनिवार्य रूप से है कि बहुत छवि के एक एक्सेल प्रतिनिधित्व में एक छवि तब्दील लिखना है के बाद बंद हो जाता है। मैं अभी क्या कर रहा हूं कि मैं छवि लोड कर रहा हूं, और मुझे छवि के लिए आरजीबी मानों को पूर्णांक के 2 डी सरणी में मिल रहा है।Apache POI शैली सेटिंग थोड़ी देर
जो समस्या मैं सामना कर रहा हूं वह यह है। मेरी कोशिकाओं में अचानक कोई स्टाइल नहीं है! पृष्ठभूमि रंग वाले कुछ कोशिकाओं के बाद, शेष सफेद छोड़ दिया जाता है, मैं 4,0000 शैलियों की सीमा से पहले नहीं जा रहा हूं क्योंकि मैं छवि को 60 * 60 रिज़ॉल्यूशन तक सीमित कर रहा हूं। तो मुझे पूरा यकीन नहीं है कि मैं क्या गलत कर रहा हूं।
मेरा मुख्य वर्ग:
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();
}
}
मेरी imageHandler वर्ग:
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;
}
}
संपादित करें: नए-नए अपडेट कोड
चालक:
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;
}
}
मेरी छवि हैंडलर वर्ग अनिवार्य हैवही, लेकिन मैं छवि का आकार बदल नहीं रहा हूँ।
यह मेरा "test.jpg"
यहाँ एक्सेल (रोटेशन अलग तरह लग रहा है की एक स्क्रीनशॉट है, मैं रंग, और अधिक जटिल कुछ भी साथ और अधिक चिंतित हूँ, और यह सिर्फ कचरा में बदल जाता है)
पूरी तरह सुनिश्चित नहीं हैं कि मुझे क्या करना चाहिए
क्या आप पैलेट में अतिरिक्त रंगों से बाहर हो सकते हैं? IIRC एक बहुत कम उन्हें – Gagravarr
@Gagravarr हम्म, मैं पूरी तरह यकीन नहीं है, मुझे पता है कि पर जाने जब मैंने कोशिश की अलग अलग रंग का उपयोग कर आप सेल शैलियों की संख्या की तुलना में परिभाषित किया है सकते हैं की संख्या पर Excel (.xls) में सीमा नहीं है 4,000 शैलियों मुझे एक रनटाइम अपवाद मिला। यदि ऐसा है, तो मुझे कोई सुझाव देना चाहिए कि मुझे कैसे आगे बढ़ना चाहिए? मैं एक मौजूदा रंग overwritting की कोशिश की और कहा कि का उपयोग करते हुए, लेकिन है कि या तो काम नहीं किया। :( –
[PaletteRecord के लिए फ़ाइल स्वरूप विशिष्टता] (http://msdn.microsoft.com/en-us/library/dd909801%28v=office.12%29.aspx) वहाँ एक में 56 रंगों की एक कठिन सीमा है पता चलता है '.xls' फ़ाइल। क्या आप XSSF/.xlsx पर स्विच कर सकते हैं?उस प्रतिबंध के बिना रंगों का एक अलग तरीका है, – Gagravarr