0

When i rescale height and width image it on rotate 90 degrees

@RequestMapping(value = "/save" , method = RequestMethod.POST, headers = "Content-Type=multipart/form-data", consumes = { "multipart/form-data" })
ResponseEntity<Object> saveBackground(@RequestParam(required = false) MultipartFile imageLeft){
     BackgroundDto backgroundDto = new BackgroundDto();
     backgroundDto.setLeftPage(imageLeft.getBytes()); --> saved right
     //saved wrong orientation
     backgroundDto.setMinLeftPage( CompressImages.compress(imageLeft.getBytes()));
     backgroundDto.setUser(user);
     backgroundRepo.save(backgroundDto);
}

public static byte[]  compress(byte[] img) throws IOException {
    InputStream inputstream = new ByteArrayInputStream(img);
    ByteArrayOutputStream baosImage = new ByteArrayOutputStream();
    ByteArrayOutputStream rescale = rescale(image);
     
  return rescale.toByteArray(); 
}

private static ByteArrayOutputStream rescale(BufferedImage bi) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int originalWidth = bi.getWidth();
    int 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();
    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;
}

backgroundDto.setLeftPage(imageLeft.getBytes()); --> saved right orientation backgroundDto.setMinLeftPage( CompressImages.compress(imageLeft.getBytes())); --> saved wrong orientation

Original image: enter image description here

image after rectange: enter image description here

mobiw
  • 89
  • 7
  • Why do draw the image before setting the Rendering hints? – akarnokd Jun 27 '22 at 17:17
  • Show code where you save image after rescaling? Because problem might be there. – Andrey Kotov Jun 27 '22 at 17:36
  • 3
    1 ) how is the original image being read? 2) how is the created output stream being saved? (BTW that is 90 or 270 degrees not180) || tested posted code, but displaying the scaled image, no rotation! Same if saving directly to file. – user16320675 Jun 27 '22 at 17:38
  • It is possible your source image has EXIF data about orientation, which might not be handled by Java properly: https://stackoverflow.com/a/47671104/61158 – akarnokd Jun 27 '22 at 17:49
  • A workaround: https://stackoverflow.com/a/29319713/61158 – akarnokd Jun 27 '22 at 17:50
  • @AndreyKotov i save into DB backgroundDto.setMinLeftPage(CompressImages.compress(imageLeft.getBytes()));) – mobiw Jun 27 '22 at 19:27
  • 1
    @mobiw what we're trying to say here in comments - is that maybe the way you're getting the image from UI and then saving it to DB and then getting it back to see result - affects image's rotation. Because if I copy your `rescale` method as it is - it works fine. You can even try it by yourself by creating new empty console project and using my code: https://pastebin.com/rDceRWjF – Andrey Kotov Jun 27 '22 at 20:14

0 Answers0