/** * @param li ImageLevelInfo object * @param con JDBC Connection * @return the resolution for li, based on a random chosen tile * @throws SQLException * @throws IOException */ protected double[] getPixelResolution(ImageLevelInfo li, Connection con) throws SQLException, IOException { double[] result = null; String statementString = getRandomTileStatement(li); PreparedStatement s = con.prepareStatement(statementString); ResultSet r = s.executeQuery(); while (r.next()) { byte[] tileBytes = getTileBytes(r); if (tileBytes == null) { continue; } BufferedImage buffImage = null; li.setCanImageIOReadFromInputStream(true); try { buffImage = ImageIO.read(new ByteArrayInputStream(tileBytes)); } catch (IOException e) { } if (buffImage == null) { if (LOGGER.isLoggable(Level.WARNING)) { LOGGER.warning( "Image IO cannot read from ByteInputStream,use less efficient jai methods"); } li.setCanImageIOReadFromInputStream(false); SeekableStream stream = new ByteArraySeekableStream(tileBytes); String decoderName = null; for (String dn : ImageCodec.getDecoderNames(stream)) { decoderName = dn; break; } ImageDecoder decoder = ImageCodec.createImageDecoder(decoderName, stream, null); PlanarImage img = PlanarImage.wrapRenderedImage(decoder.decodeAsRenderedImage()); buffImage = img.getAsBufferedImage(); } Envelope env = getEnvelopeFromResultSet(r); result = new double[] { env.getWidth() / buffImage.getWidth(), env.getHeight() / buffImage.getHeight() }; break; } r.close(); s.close(); return result; }
/** * A convenience method for registering XTIFF methods into JAI, for extended classes of * XTIFFDescriptor. */ public static void register(XTIFFDescriptor odesc) { OperationRegistry reg = JAI.getDefaultInstance().getOperationRegistry(); // override tiff operation reg.unregisterOperationDescriptor("tiff"); // ...and register tiff with the new desc reg.registerOperationDescriptor(odesc, "tiff"); reg.registerRIF("tiff", "org.libtiff.jai", odesc); // re-register the tiff codec ImageCodec.unregisterCodec("tiff"); ImageCodec.registerCodec(new XTIFFCodec()); }
/** * Sets up all the images and trackers. * * @throws IOException if the image cannot be loaded */ public static void settupImages() throws IOException { FileSeekableStream stream = new FileSeekableStream(filePath); ImageDecodeParam param = null; ImageDecoder dec = ImageCodec.createImageDecoder("tiff", stream, param); images = new RenderedImage[TIFFDirectory.getNumDirectories(stream)]; System.out.println(images.length); for (int i = 0; i < images.length; i++) { images[i] = new NullOpImage(dec.decodeAsRenderedImage(i), null, null, OpImage.OP_IO_BOUND); } JFrame f = new JFrame("Trak"); im = new ImageFrame(images); File save = new File(filePath + ".trak"); if (save.exists()) { try { ObjectInputStream fromSave = new ObjectInputStream(new GZIPInputStream(new FileInputStream(save))); trackers = (DotTracker[]) fromSave.readObject(); if (trackers.length != images.length) { throw new IOException("Invalid Trackers"); } foundValidTrackers = true; System.out.println("Valid trackers found, not re-refining data"); } catch (Exception e) { } } if (!foundValidTrackers) { trackers = new DotTracker[images.length]; } f.add(im); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setLocationRelativeTo(null); for (int i = 0; i < images.length; i++) { if (!foundValidTrackers) { trackers[i] = new DotTracker(im.datas[i]); } trackers[i].colorRefined(im.refinedImages[i].getGraphics()); System.out.println(i); } remakeDots(); f.setVisible(true); }
public void write(RenderedImage renderedImage, String contentType, OutputStream os) throws IOException { if (contentType.contains(TYPE_BMP)) { ImageEncoder imageEncoder = ImageCodec.createImageEncoder(TYPE_BMP, os, null); imageEncoder.encode(renderedImage); } else if (contentType.contains(TYPE_GIF)) { encodeGIF(renderedImage, os); } else if (contentType.contains(TYPE_JPEG) || contentType.contains("jpeg")) { ImageIO.write(renderedImage, "jpeg", os); } else if (contentType.contains(TYPE_PNG)) { ImageIO.write(renderedImage, TYPE_PNG, os); } else if (contentType.contains(TYPE_TIFF) || contentType.contains("tif")) { ImageEncoder imageEncoder = ImageCodec.createImageEncoder(TYPE_TIFF, os, null); imageEncoder.encode(renderedImage); } }
public ImageBag read(byte[] bytes) { RenderedImage renderedImage = null; String type = TYPE_NOT_AVAILABLE; Enumeration<ImageCodec> enu = ImageCodec.getCodecs(); while (enu.hasMoreElements()) { ImageCodec codec = enu.nextElement(); if (codec.isFormatRecognized(bytes)) { type = codec.getFormatName(); renderedImage = read(bytes, type); break; } } if (type.equals("jpeg")) { type = TYPE_JPEG; } return new ImageBag(renderedImage, type); }
protected RenderedImage read(byte[] bytes, String type) { RenderedImage renderedImage = null; try { if (type.equals(TYPE_JPEG)) { type = "jpeg"; } ImageDecoder decoder = ImageCodec.createImageDecoder(type, new UnsyncByteArrayInputStream(bytes), null); renderedImage = decoder.decodeAsRenderedImage(); } catch (IOException ioe) { if (_log.isDebugEnabled()) { _log.debug(type + ": " + ioe.getMessage()); } } return renderedImage; }