private void roundtrip( FormatInfo formatInfo, BufferedImage testImage, String tempPrefix, boolean imageExact) throws IOException, ImageReadException, ImageWriteException { File temp1 = createTempFile(tempPrefix + ".", "." + formatInfo.format.extension); // Debug.debug("tempFile: " + tempFile.getName()); Map params = new HashMap(); Sanselan.writeImage(testImage, temp1, formatInfo.format, params); Map readParams = new HashMap(); readParams.put(SanselanConstants.BUFFERED_IMAGE_FACTORY, new RgbBufferedImageFactory()); BufferedImage image2 = Sanselan.getBufferedImage(temp1, readParams); assertNotNull(image2); if (imageExact) { // note tolerance when comparing grayscale images // BufferedImages of compareImagesExact(testImage, image2); } File temp2 = createTempFile(tempPrefix + ".", "." + formatInfo.format.extension); // Debug.debug("tempFile: " + tempFile.getName()); Sanselan.writeImage(image2, temp2, formatInfo.format, params); compareFilesExact(temp1, temp2); }
@Override public void _setOkBadge(IdeFrame frame, boolean visible) { if (!isValid(frame)) { return; } Object icon = null; if (visible) { synchronized (Win7AppIcon.class) { if (myOkIcon == null) { try { BufferedImage image = ImageIO.read(getClass().getResource("/mac/appIconOk512.png")); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); Sanselan.writeImage(image, bytes, ImageFormat.IMAGE_FORMAT_ICO, new HashMap()); myOkIcon = Win7TaskBar.createIcon(bytes.toByteArray()); } catch (Throwable e) { LOG.error(e); myOkIcon = null; } } icon = myOkIcon; } } try { Win7TaskBar.setOverlayIcon(frame, icon, false); } catch (Throwable e) { LOG.error(e); } }
/** * translate a binary array to a buffered image * * @param binary * @return * @throws IOException */ @Override public final BufferedImage toBufferedImage(byte[] bytes, String format) throws IOException { try { return Sanselan.getBufferedImage(new ByteArrayInputStream(bytes)); } catch (ImageReadException e) { throw ExceptionUtil.toIOException(e); } }
/** * translate a file resource to a buffered image * * @param res * @return * @throws IOException */ @Override public final BufferedImage toBufferedImage(Resource res, String format) throws IOException { InputStream is = null; try { return Sanselan.getBufferedImage(is = res.getInputStream()); } catch (ImageReadException e) { throw ExceptionUtil.toIOException(e); } finally { IOUtil.closeEL(is); } }
public void test() throws Exception { List images = getImagesWithExifData(300); for (int i = 0; i < images.size(); i++) { if (i % 10 == 0) Debug.purgeMemory(); File imageFile = (File) images.get(i); // Debug.debug(); // Debug.debug("imageFile", imageFile); if (imageFile.getParentFile().getName().toLowerCase().equals("@broken")) continue; try { Map params = new HashMap(); boolean ignoreImageData = isPhilHarveyTestImage(imageFile); params.put(PARAM_KEY_READ_THUMBNAILS, new Boolean(!ignoreImageData)); JpegImageMetadata metadata = (JpegImageMetadata) Sanselan.getMetadata(imageFile, params); if (null == metadata) continue; TiffImageMetadata exifMetadata = metadata.getExif(); if (null == exifMetadata) continue; TiffImageMetadata.GPSInfo gpsInfo = exifMetadata.getGPS(); if (null == gpsInfo) continue; Debug.debug("imageFile", imageFile); Debug.debug("gpsInfo", gpsInfo); Debug.debug("gpsInfo longitude as degrees east", gpsInfo.getLongitudeAsDegreesEast()); Debug.debug("gpsInfo latitude as degrees north", gpsInfo.getLatitudeAsDegreesNorth()); Debug.debug(); } catch (Exception e) { Debug.debug("imageFile", imageFile.getAbsoluteFile()); Debug.debug("imageFile", imageFile.length()); Debug.debug(e, 13); // File brokenFolder = new File(imageFile.getParentFile(), "@Broken"); // if(!brokenFolder.exists()) // brokenFolder.mkdirs(); // File movedFile = new File(brokenFolder, imageFile.getName()); // imageFile.renameTo(movedFile); throw e; } } }
private void readInfo() { try { IImageMetadata metadata = Sanselan.getMetadata(source); if (metadata instanceof JpegImageMetadata) { JpegImageMetadata jpegMeta = (JpegImageMetadata) metadata; TiffField exifDate = jpegMeta.findEXIFValue(TiffConstants.EXIF_TAG_DATE_TIME_ORIGINAL); if (exifDate != null) { String date = (String) exifDate.getValue(); dateTaken = parseExifDate(date); successful = true; } } } catch (ImageReadException e) { } catch (IOException e) { throw new RuntimeException("IO Error while getting metadata: " + e.getMessage(), e); } }
@Override public void _setTextBadge(IdeFrame frame, String text) { if (!isValid(frame)) { return; } Object icon = null; if (text != null) { try { int size = 55; BufferedImage image = UIUtil.createImage(size, size, BufferedImage.TYPE_INT_ARGB); Graphics2D g = image.createGraphics(); int roundSize = 40; g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setPaint(ERROR_COLOR); g.fillRoundRect( size / 2 - roundSize / 2, size / 2 - roundSize / 2, roundSize, roundSize, size, size); g.setColor(Color.white); Font font = g.getFont(); g.setFont(new Font(font.getName(), font.getStyle(), 22)); FontMetrics fontMetrics = g.getFontMetrics(); int width = fontMetrics.stringWidth(text); g.drawString( text, size / 2 - width / 2, size / 2 - fontMetrics.getHeight() / 2 + fontMetrics.getAscent()); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); Sanselan.writeImage(image, bytes, ImageFormat.IMAGE_FORMAT_ICO, new HashMap()); icon = Win7TaskBar.createIcon(bytes.toByteArray()); } catch (Throwable e) { LOG.error(e); } } try { Win7TaskBar.setOverlayIcon(frame, icon, icon != null); } catch (Throwable e) { LOG.error(e); } }
/** * We don't actually rewrite the image we just ensure that it is in fact a valid and known image * type. */ private boolean rewriteProxiedImage( HttpRequest request, HttpResponse resp, MutableContent content) { boolean imageIsSafe = false; try { String contentType = resp.getHeader("Content-Type"); if (contentType == null || contentType.toLowerCase().startsWith("image/")) { // Unspecified or unknown image mime type. try { ImageFormat imageFormat = Sanselan.guessFormat( new ByteSourceInputStream(resp.getResponse(), request.getUri().getPath())); if (imageFormat == ImageFormat.IMAGE_FORMAT_UNKNOWN) { logger.log( Level.INFO, "Unable to sanitize unknown image type " + request.getUri().toString()); return true; } imageIsSafe = true; // Return false to indicate that no rewriting occurred return false; } catch (IOException ioe) { throw new RuntimeException(ioe); } catch (ImageReadException ire) { // Unable to read the image so its not safe logger.log( Level.INFO, "Unable to detect image type for " + request.getUri().toString() + " for sanitized content", ire); return true; } } else { return true; } } finally { if (!imageIsSafe) { content.setContent(""); } } }
protected SanselanCoder() { super(); Sanselan.hasImageFileExtension("lucee.gif"); // to make sure Sanselan exist when load this class }
protected static boolean isJpeg(File file) throws IOException, ImageReadException { ImageFormat format = Sanselan.guessFormat(file); return format == ImageFormat.IMAGE_FORMAT_JPEG; }
@Override public Document handleDocument(IndexWriter indexWriter, IndexState state, File file) throws Exception { Document doc = super.handleDocument(indexWriter, state, file); /* create/update document */ if (doc != null && file.length() > 0) { /* ultra fast header analysis, with fileinputstream - too many filedescripor * exeption on samba network */ try { ImageInfo imageInfo = Sanselan.getImageInfo(file); /* default image meta informations */ int w = imageInfo.getWidth(); int h = imageInfo.getHeight(); /* image width */ get(ImageWidthField.NAME).add(doc, w); /* image height */ get(ImageHeightField.NAME).add(doc, h); /* bits per pixel */ get(BitsPerPixelField.NAME).add(doc, imageInfo.getBitsPerPixel()); /* complete size */ get(PixelSizeField.NAME).add(doc, w * h); /* image aspect ratio */ get(AspectRatioField.NAME).add(doc, w / (float) h); /* try to extract metadata from jpeg files */ String mime = imageInfo.getMimeType(); /* override because they are different content types than extensions */ doc.removeFields(MimeTypeField.NAME); get(MimeTypeField.NAME).add(doc, mime); /* special jpeg handling */ if (mime != null && mime.equals("image/jpeg")) { IImageMetadata metadata = Sanselan.getMetadata(file); if (metadata instanceof JpegImageMetadata) { JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata; /* create date of the image */ TiffField field = jpegMetadata.findEXIFValue(ExifTagConstants.EXIF_TAG_CREATE_DATE); if (field != null) { Date date = DateUtil.parseEXIFFormat(field.getStringValue()); /* else unknown format */ if (date != null) { /* override file last modified date */ doc.removeFields(LastModifiedField.NAME); get(LastModifiedField.NAME).add(doc, date.getTime()); } } /* user comment tag */ field = jpegMetadata.findEXIFValue(ExifTagConstants.EXIF_TAG_USER_COMMENT); if (field != null) { get(CommentField.NAME).add(doc, field.getStringValue().trim()); } /* make tag */ field = jpegMetadata.findEXIFValue(ExifTagConstants.EXIF_TAG_MAKE); if (field != null) { get(ExifMakeField.NAME).add(doc, field.getStringValue().trim()); } /* model tag */ field = jpegMetadata.findEXIFValue(ExifTagConstants.EXIF_TAG_MODEL); if (field != null) { get(ExifModelField.NAME).add(doc, field.getStringValue().trim()); } /* date time tag */ field = jpegMetadata.findEXIFValue(ExifTagConstants.EXIF_TAG_CREATE_DATE); if (field != null) { Date date = DateUtil.parseEXIFFormat(field.getStringValue()); /* else unknown format */ if (date != null) { get(ExifDateField.NAME).add(doc, date.getTime()); } } /* try to find gps informations */ TiffImageMetadata exifMetadata = jpegMetadata.getExif(); if (exifMetadata != null) { try { TiffImageMetadata.GPSInfo gpsInfo = exifMetadata.getGPS(); if (null != gpsInfo) { get(LatField.NAME).add(doc, gpsInfo.getLatitudeAsDegreesNorth()); get(LonField.NAME).add(doc, gpsInfo.getLongitudeAsDegreesEast()); // doc.add(new // NumericField(FIELD_EXIF_GPS_IMG_DIRECTION, Field.Store.YES, // true).setIntValue(gps.getRational( // // GpsDirectory.TAG_GPS_IMG_DIRECTION).intValue())); // // System.out.println(exifMetadata.findField(GPSTagConstants.GPS_TAG_GPS_IMG_DIRECTION)); } } catch (ImageReadException e) { } } } } /* run some classification */ if (classifyImage) { int[] v = ImageUtil.analyzeRGB(file); for (int i = 0; i < v.length; i++) { doc.add(new IntPoint(COLORMEAN + i, v[i])); } } } catch (Throwable e) { /* only log */ log.log(Level.INFO, "can not extract image informations!", e); } } return doc; }
public static BufferedImage readBmp(InputStream is) throws ImageReadException, IOException { return Sanselan.getBufferedImage(is); }