import javax.swing.*; import java.awt.*; import java.awt.image.ImageObserver; import java.awt.image.PixelGrabber; import java.io.*; public class HTMLImageGenerator implements ImageObserver { private static String _bgColor = "ffffff"; private static String LINE_SEP = System.getProperty("line.separator"); private final static String COLOR_BEGIN_TAG = "= 3) { String chooseGray = args[2]; if (chooseGray.equalsIgnoreCase("true")) { gray = true; } } if (args.length == 4) { _words = readInFile(args[3]); String x = new String(_words); x = x.replace((char) 13, (char) 32); x = x.replace((char) 10, (char) 32); x = x.replace((char) 9, (char) 32); _words = x.getBytes(); } HTMLImageGenerator gen = new HTMLImageGenerator(image, fileName, gray); System.exit(0); } private static void bailOut() { System.out.println(USEAGE); System.exit(-1); } private static void bailOut(Exception ex) { System.out.println(USEAGE + "Exception : \n" + ex); System.exit(-1); } private static void bailOut(String error, Exception ex) { System.out.println(USEAGE + ": \n" + error + "\n" + "Exception : \n" + ex); System.exit(-1); } public HTMLImageGenerator(String imageFileName, String fileName, boolean grayScale) { _grayScale = grayScale; _output = new StringBuffer(); _baseImage = new ImageIcon(imageFileName).getImage(); _height = _baseImage.getHeight(this); _width = _baseImage.getWidth(this); if (_height <= 0 || _width <= 0) { bailOut("The file you entered :\""+fileName+"\" does not exist or is not a valid Image file.", new IllegalArgumentException("The file you entered does not exist")); } try { _outputStream = new FileOutputStream(new File(fileName)); _outputStream.write(getHeader().getBytes()); _filter = new GrayFilter(true, 40); _output = processImage(_baseImage); _outputStream.write(getFooter().getBytes()); } catch(IOException ioe) { bailOut(ioe); } } public HTMLImageGenerator(String fileName) { this(fileName, "c:\\test.htm", false); } private String getOutput() { return _output.toString(); } private static String getHeader() { return "test

"+ "
"; } private static String getFooter() { return "
"; } private StringBuffer processImage(Image image) { StringBuffer buffer = new StringBuffer(); int currentWidth = 0; int currentHeight = 0; int[] pixels = new int[_width * _height]; PixelGrabber pg = new PixelGrabber(image, 0, 0, _width, _height, pixels, 0, _width); // Go along the row, then add a break. while (currentHeight != _height) { while (currentWidth != _width) { String currentPixelString = processPixel(pg, pixels[currentHeight * _width + currentWidth]); buffer.append(currentPixelString); currentWidth++; } buffer.append(LINE_BREAK); if(buffer.length() > 65536) { try { _outputStream.write(buffer.toString().getBytes()); buffer = new StringBuffer(); } catch (IOException ex) { bailOut(ex); } } currentHeight++; currentWidth = 0; } try { _outputStream.write(buffer.toString().getBytes()); buffer = new StringBuffer(); } catch (IOException ex) { bailOut(ex); } return buffer; } private int _wordCount = 0; private String processPixel(PixelGrabber currentPixel, int pixel) { String currentPixelString = null; String currentWord = "88"; if (_words != null) { char a = (char) _words[_wordCount++ % _words.length]; while (a == 32) { a = (char) _words[_wordCount++ % _words.length]; } char b = (char) _words[_wordCount++ % _words.length]; while (b == 32) { b = (char) _words[_wordCount++ % _words.length]; } currentWord = new String(new char[]{a, b}); } try { currentPixel.grabPixels(); } catch (Exception e) { bailOut(e); } int alpha = (pixel >> 24) & 0xff; int redValue = 0; int greenValue = 0; int blueValue = 0; if (_grayScale) { pixel = _filter.filterRGB(1, 1, pixel); } redValue = (pixel >> 16) & 0xff; greenValue = (pixel >> 8) & 0xff; blueValue = (pixel) & 0xff; String R = Integer.toHexString(redValue); String G = Integer.toHexString(greenValue); String B = Integer.toHexString(blueValue); if (R.length() == 1) { R = "0" + R; } if (G.length() == 1) { G = "0" + G; } if (B.length() == 1) { B = "0" + B; } // If the pixel is transparent, set it to the bgcolor. if ("ffffff".equals(R + G + B)) { R = _bgColor.substring(0, 2); G = _bgColor.substring(0, 2); B = _bgColor.substring(0, 2); } currentPixelString = COLOR_BEGIN_TAG + HASH + R + G + B + "\">" + currentWord + COLOR_END_TAG + LINE_SEP; return currentPixelString; } public boolean imageUpdate(Image a, int b, int c, int d, int e, int f) { // nop- Fulfill interface. return true; } public OutputStream getOutputStream() { return _outputStream; } public static byte[] readInFile(String fileName) { FileInputStream in = null; byte[] reader = null; try { File readFile = new File(fileName); in = new FileInputStream(readFile); int readInt = 0; int fileLength = (int) readFile.length(); reader = new byte[fileLength]; while (readInt != -1) { readInt = in.read(reader, readInt, fileLength - readInt); } in.close(); } catch (FileNotFoundException fnfe) { } catch (IOException ioe) { } // End try-catch return reader; } // End method }