Ejemplo n.º 1
0
 /** @see nextapp.echo2.app.StreamImageReference#render(java.io.OutputStream) */
 public void render(OutputStream out) throws IOException {
   if (isKeptInMemory()) {
     byte[] bytes = (byte[]) refEncodedBytes.get();
     //
     // the GC may have reaped out bytes or the image may
     // no longer be valid.  Either way we need to encode.
     //
     if (bytes == null || !isValid()) {
       Image image = getImage();
       if (image == null) return;
       ByteArrayOutputStream ba = new ByteArrayOutputStream();
       encoder.encode(image, ba);
       bytes = ba.toByteArray();
       refEncodedBytes = new SoftReference(bytes);
       setValid(true);
       //
       // tell any imagelisteners that the image has changed
       update();
     }
     out.write(bytes);
   } else {
     Image image = getImage();
     if (image == null) return;
     encoder.encode(image, out);
   }
 }
 public void testPngEncoderAvailable() throws UnsupportedEncoderException, IOException {
   assertTrue(ImageEncoderRegistry.getInstance().isEncoderAvailable("image/png"));
   final ImageEncoder imageEncoder = ImageEncoderRegistry.getInstance().createEncoder("image/png");
   assertNotNull(imageEncoder);
   final ByteArrayOutputStream bout = new ByteArrayOutputStream();
   imageEncoder.encodeImage(
       new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB), bout, 0.75f, false);
   assertTrue(bout.toByteArray().length > 0);
 }
Ejemplo n.º 3
0
 /**
  * Encode the image in a specific format and write it to an OutputStream.
  *
  * @param image The image to be encoded.
  * @param format The {@link ImageFormat} to use.
  * @param outputStream The OutputStream to write the encoded image to.
  * @param quality The quality to use for the image encoding (not supported by all ImageEncoders).
  * @param encodeAlpha Whether to encode alpha transparency (not supported by all ImageEncoders).
  * @throws IOException if there is an IO problem.
  */
 public static void writeBufferedImage(
     BufferedImage image,
     String format,
     OutputStream outputStream,
     float quality,
     boolean encodeAlpha)
     throws IOException {
   ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format, quality, encodeAlpha);
   imageEncoder.encode(image, outputStream);
 }
Ejemplo n.º 4
0
  public static byte[] tryToDecodeSteganoImage(byte[] bytes, String key)
      throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
          InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
    try {
      // 1. Try to load as image
      BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes));

      // 2. Try to read stegano data
      short[] s = ImageEncoder.readBytesFromImage(img, key);

      if (s == null) {
        return null;
      }

      // Data inside image is too small, reject image
      if (s.length < EncryptionProvider.SHA_256_HASH_SIZE_BYTES * 2) {
        return null;
      }

      byte[] b = new byte[s.length];
      for (int i = 0; i < b.length; i++) {
        b[i] = (byte) (s[i] & 0xFF);
      }

      // 3. Try to decrypt bytes
      byte[] decryptedBytes = null;
      decryptedBytes = EncryptionProvider.decryptBytes(b, key.getBytes());

      // 4. Try to decompress bytes
      decryptedBytes = CompressionProvider.decompressBytes(decryptedBytes);

      return decryptedBytes;
    } catch (DataFormatException | IOException ex) {
      ex.printStackTrace();
    }

    return null;
  }
Ejemplo n.º 5
0
  public static void encodeIntoImage(
      File containerImg, File outputFile, byte[] srcBytes, String key) {
    try {
      BufferedImage in = ImageIO.read(containerImg);
      BufferedImage newImage =
          new BufferedImage(in.getWidth(), in.getHeight(), BufferedImage.TYPE_INT_ARGB);
      Graphics2D g = newImage.createGraphics();
      g.drawImage(in, 0, 0, null);
      g.dispose();

      srcBytes = CompressionProvider.compressBytes(srcBytes);

      byte[] encryptedBytes = EncryptionProvider.encryptBytes(srcBytes, key.getBytes());

      // Trick: convert from unsigned number to signed byte
      short[] srcS = new short[encryptedBytes.length];
      for (int i = 0; i < encryptedBytes.length; i++) {
        srcS[i] = (short) (encryptedBytes[i] & 0xFF);
      }

      ImageEncoder.writeBytesToImage(in, encryptedBytes, outputFile.toString(), key);

      System.out.println("[OK] Steganographic .png \"" + outputFile.toString() + "\" generated.");
    } catch (DataFormatException ex) {
      Logger.getLogger(ImageUtils.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoSuchAlgorithmException
        | NoSuchPaddingException
        | InvalidKeyException
        | InvalidAlgorithmParameterException
        | IllegalBlockSizeException
        | BadPaddingException
        | ImageWriteException
        | IOException ex) {
      Logger.getLogger(ImageUtils.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
Ejemplo n.º 6
0
 /** @see java.io.Serializable */
 private void writeObject(ObjectOutputStream out) throws IOException {
   out.defaultWriteObject();
   out.writeObject(encoder.getClass());
   ImageKit.writeSerializedImage(out, getImage());
 }
Ejemplo n.º 7
0
 /** @see nextapp.echo2.app.StreamImageReference#getContentType() */
 public String getContentType() {
   return encoder.getContentType();
 }
Ejemplo n.º 8
0
 /**
  * Encode the image in a specific format.
  *
  * @param image The image to be encoded.
  * @param format The {@link ImageFormat} to use.
  * @param quality The quality to use for the image encoding (not supported by all ImageEncoders).
  * @return The byte[] that is the encoded image.
  * @throws IOException if there is an IO problem.
  */
 public static byte[] encode(BufferedImage image, String format, float quality)
     throws IOException {
   ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format, quality);
   return imageEncoder.encode(image);
 }
Ejemplo n.º 9
0
 /**
  * Encode the image in a specific format.
  *
  * @param image The image to be encoded.
  * @param format The {@link ImageFormat} to use.
  * @param encodeAlpha Whether to encode alpha transparency (not supported by all ImageEncoders).
  * @return The byte[] that is the encoded image.
  * @throws IOException if there is an IO problem.
  */
 public static byte[] encode(BufferedImage image, String format, boolean encodeAlpha)
     throws IOException {
   ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format, encodeAlpha);
   return imageEncoder.encode(image);
 }
Ejemplo n.º 10
0
 /**
  * Encode the image in a specific format and write it to an OutputStream.
  *
  * @param image The image to be encoded.
  * @param format The {@link ImageFormat} to use.
  * @param outputStream The OutputStream to write the encoded image to.
  * @throws IOException if there is an IO problem.
  */
 public static void writeBufferedImage(
     BufferedImage image, String format, OutputStream outputStream) throws IOException {
   ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format);
   imageEncoder.encode(image, outputStream);
 }