- /**
- * Convert the given {@link Image} object into a Base64 representation of
- * the same {@link Image}. object.
- *
- * @param image
- * the {@link Image} object to convert
- *
- * @return the Base64 representation
- *
- * @throws IOException
- * in case of IO error
- */
- static public String fromImage(BufferedImage image) throws IOException {
- String imageString = null;
- ByteArrayOutputStream out = new ByteArrayOutputStream();
-
- ImageIO.write(image, "jpeg", out);
- byte[] imageBytes = out.toByteArray();
-
- imageString = new String(Base64.getEncoder().encode(imageBytes));
-
- out.close();
-
- return imageString;
- }
-
- /**
- * Convert the given {@link File} image into a Base64 representation of the
- * same {@link File}.
- *
- * @param file
- * the {@link File} image to convert
- *
- * @return the Base64 representation
- *
- * @throws IOException
- * in case of IO error
- */
- static public String fromStream(InputStream in) throws IOException {
- String fileString = null;
- ByteArrayOutputStream out = new ByteArrayOutputStream();
-
- byte[] buf = new byte[8192];
-
- int c = 0;
- while ((c = in.read(buf, 0, buf.length)) > 0) {
- out.write(buf, 0, c);
- }
- out.flush();
- in.close();
-
- fileString = new String(Base64.getEncoder().encode(out.toByteArray()));
- out.close();
-
- return fileString;
- }
-
- /**
- * Convert the given Base64 representation of an image into an {@link Image}
- * object.
- *
- * @param b64data
- * the {@link Image} in Base64 format
- *
- * @return the {@link Image} object
- *
- * @throws IOException
- * in case of IO error
- */
- static public BufferedImage toImage(String b64data) throws IOException {
- ByteArrayInputStream in = new ByteArrayInputStream(Base64.getDecoder()
- .decode(b64data));
- return toImage(in);
- }
-
- /**
- * Convert the given {@link InputStream} (which must allow calls to
- * {@link InputStream#reset()}) into an {@link Image} object.
- *
- * @param in
- * the 'resetable' {@link InputStream}
- *
- * @return the {@link Image} object
- *
- * @throws IOException
- * in case of IO error
- */
- static public BufferedImage toImage(InputStream in) throws IOException {
- int orientation;
- try {
- orientation = getExifTransorm(in);
- } catch (Exception e) {
- // no EXIF transform, ok
- orientation = -1;
- }
-
- in.reset();
- BufferedImage image = ImageIO.read(in);
-
- if (image == null) {
- throw new IOException("Failed to convert input to image");
- }
-
- // Note: this code has been found on internet;
- // thank you anonymous coder.
- int width = image.getWidth();
- int height = image.getHeight();
- AffineTransform affineTransform = new AffineTransform();
-
- switch (orientation) {
- case 1:
- break;
- case 2: // Flip X
- affineTransform.scale(-1.0, 1.0);
- affineTransform.translate(-width, 0);
- break;
- case 3: // PI rotation
- affineTransform.translate(width, height);
- affineTransform.rotate(Math.PI);
- break;
- case 4: // Flip Y
- affineTransform.scale(1.0, -1.0);
- affineTransform.translate(0, -height);
- break;
- case 5: // - PI/2 and Flip X
- affineTransform.rotate(-Math.PI / 2);
- affineTransform.scale(-1.0, 1.0);
- break;
- case 6: // -PI/2 and -width
- affineTransform.translate(height, 0);
- affineTransform.rotate(Math.PI / 2);
- break;
- case 7: // PI/2 and Flip
- affineTransform.scale(-1.0, 1.0);
- affineTransform.translate(-height, 0);
- affineTransform.translate(0, width);
- affineTransform.rotate(3 * Math.PI / 2);
- break;
- case 8: // PI / 2
- affineTransform.translate(0, width);
- affineTransform.rotate(3 * Math.PI / 2);
- break;
- default:
- affineTransform = null;
- break;
- }
-
- if (affineTransform != null) {
- AffineTransformOp affineTransformOp = new AffineTransformOp(
- affineTransform, AffineTransformOp.TYPE_BILINEAR);
-
- BufferedImage transformedImage = new BufferedImage(height, width,
- image.getType());
- transformedImage = affineTransformOp
- .filter(image, transformedImage);
-
- image = transformedImage;
- }
- //
-
- return image;
- }
-