Total Pageviews

2019/12/04

[Java] How to magnify and shrink image?

Scenario


How-To
Here has sample code:
  package test.util;
  
  import java.awt.Graphics;
  import java.awt.image.BufferedImage;
  import java.io.File;
  import java.io.IOException;
  
  import javax.imageio.ImageIO;
  
  import org.apache.commons.io.FilenameUtils;
  import org.springframework.stereotype.Component;
  
  import lombok.extern.slf4j.Slf4j;
  
  @Slf4j
  @Component
  public class ImageUtils {
  
      /**
       * Magnify image
       * 
       * @param oFile
       *            original file
       * @param times
       *            magnification times
       * @param mFile
       *            magnified image
       * @throws IOException
       */
      public void magnifyImage(File oFile, Integer times, File mFile) throws IOException {
          try {
              BufferedImage originalImage = ImageIO.read(oFile);
  
              int width = originalImage.getWidth() * times;
              int height = originalImage.getHeight() * times;
  
              BufferedImage newImage = new BufferedImage(width, height, originalImage.getType());
              Graphics g = newImage.getGraphics();
              g.drawImage(originalImage, 0, 0, width, height, null);
              g.dispose();
  
              ImageIO.write(newImage, FilenameUtils.getExtension(mFile.getName()), mFile);
              log.debug("magnified image successfully to {}", mFile.getAbsolutePath());
          } catch (IOException e) {
              throw new IOException("fail to magnify image", e);
          }
      }
  
      /**
       * Shrink image
       * 
       * @param oFile
       *            original file
       * @param times
       *            shrink times
       * @param sFile
       *            shrinked image
       * @throws IOException
       */
      public void shrinkImage(File oFile, Integer times, File sFile) throws IOException {
          try {
              BufferedImage originalImage = ImageIO.read(oFile);
              int width = originalImage.getWidth() / times;
              int height = originalImage.getHeight() / times;
  
              BufferedImage newImage = new BufferedImage(width, height, originalImage.getType());
              Graphics g = newImage.getGraphics();
              g.drawImage(originalImage, 0, 0, width, height, null);
              g.dispose();
  
              ImageIO.write(newImage, FilenameUtils.getExtension(sFile.getName()), sFile);
              log.debug("shinked image successfully to {}", sFile.getAbsolutePath());
          } catch (IOException e) {
              throw new IOException("fail to shrink image", e);
          }
      }
      
  }
  


Test class:
  package test.util;
  
  import static org.assertj.core.api.Assertions.assertThat;
  
  import java.io.File;
  import java.io.IOException;
  
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.test.context.junit4.SpringRunner;
  
  @RunWith(SpringRunner.class)
  @SpringBootTest
  public class ImageUtilsTest {
      
      private File originalFile = new File("C:\\Users\\user\\Downloads\\originalFile.jpg");
      private File shrinkFile = new File("C:\\Users\\user\\Downloads\\shrinkFile.jpg");
      private File magnificationFile = new File("C:\\Users\\user\\Downloads\\manificationFile.jpg");
      
      @Autowired
      private ImageUtils imageUtils;
      
      @Test
      public void testShrinkImage() throws IOException {
          imageUtils.shrinkImage(originalFile, 10, shrinkFile);
          assertThat(shrinkFile.exists()).isTrue();
      }
  
      @Test
      public void testMagnifyImage() throws IOException {
          imageUtils.magnifyImage(shrinkFile, 2, magnificationFile);
          assertThat(magnificationFile.exists()).isTrue();
      }
      
  }
  



No comments: