0

When i rotate BufferedImage i get black image.

I need rectangle image, and rotate image if image has EXIF data about orientation(ImageIO.read( ) always rotates my uploaded picture) But when i rotate image it became black If I don't rotate the image everything works.

   public static byte[]  compress(byte[] img) throws IOException, ImageProcessingException, MetadataException {


       ByteArrayOutputStream rescale = rescale(img);


       return rescale.toByteArray();

   }

   private static Double getOrientation(InputStream inputstream) throws ImageProcessingException, IOException, MetadataException {
       Metadata metadata = ImageMetadataReader.readMetadata(inputstream);

       ExifIFD0Directory exifIFD0 = metadata.getDirectory(ExifIFD0Directory.class);
       int orientation = exifIFD0.getInt(ExifIFD0Directory.TAG_ORIENTATION);

       switch (orientation) {
           case 1: // [Exif IFD0] Orientation - Top, left side (Horizontal / normal)
               return null;
           case 6: // [Exif IFD0] Orientation - Right side, top (Rotate 90 CW)
               return 90d;
           case 3: // [Exif IFD0] Orientation - Bottom, right side (Rotate 180)
               return 180d;
           case 8: // [Exif IFD0] Orientation - Left side, bottom (Rotate 270 CW)
               return 270d;
       }
       return null;

   }
    private static ByteArrayOutputStream rescale(byte[] img) throws IOException {

        InputStream inputstreamMeta = new ByteArrayInputStream(img);
        InputStream inputstream = new ByteArrayInputStream(img);

        Double orientation1 = null;
        try {
            orientation1 = getOrientation(inputstreamMeta);
        } catch (ImageProcessingException | MetadataException | IOException e) {

        }
        BufferedImage bi = ImageIO.read(inputstream);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        
        int originalWidth = 0;
        int originalHeight = 0;
        if(orientation1!=null && (orientation1 == 90 || orientation1 == 270)) {
            originalWidth = bi.getHeight();
            originalHeight = bi.getWidth();
        }else {
             originalWidth = bi.getWidth();
             originalHeight = bi.getHeight();
        }
        int type = bi.getType() == 0? BufferedImage.TYPE_INT_ARGB : bi.getType();

        //rescale 50%
        BufferedImage resizedImage = new BufferedImage(originalWidth/4, originalHeight/4, type);
        Graphics2D g = resizedImage.createGraphics();

          if(orientation1!=null) {
            g.rotate(orientation1);
        }
        g.drawImage(bi, 0, 0, originalWidth/4, originalHeight/4, null);
      
        g.dispose();
        g.setComposite(AlphaComposite.Src);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);




        ImageIO.write(resizedImage, "jpg",baos);

        return baos;
    }

original image:

enter image description here

after rorate and rectangle enter image description here

mobiw
  • 89
  • 7
  • You create a blank buffered image `resizedImage`, then you write a blank `ByteArrayOutputStream` to it `ImageIO.write(resizedImage, "jpg",baos);`, so you get the default black image with no content. Make sure you write an actual image. See duplicate question: [BufferedImage displaying as only black](https://stackoverflow.com/questions/27660366/bufferedimage-displaying-as-only-black) – sorifiend Jun 29 '22 at 00:02
  • Personally, I'd just make use of the `Graphics2D` API - for [example](https://stackoverflow.com/questions/37758061/rotate-a-buffered-image-in-java/37758533#37758533) – MadProgrammer Jun 29 '22 at 00:23
  • 1
    When you rotate around (0,0) corner, data tends to be outside the image. Further note that there is no point in setting rendering hints *after* you performed the operation, not to speak of setting them after dispose(). Btw transforming a `BufferedImage` into another can be a one-liner when using [`AffineTransformOp`](https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/java/awt/image/AffineTransformOp.html) and [`getQuadrantRotateInstance`](https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/java/awt/geom/AffineTransform.html#getQuadrantRotateInstance(int,double,double)) – Holger Jun 29 '22 at 14:22

0 Answers0