How to scale an image using Java



Problem Description

How to scale an image using Java.

Solution

Following is the program to scale an image using Java.

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;

import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class ScalingAnImage {
   public static void main(String args[]) { 
      
      //Loading the OpenCV core library
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
      
      //Reading the Image from the file and storing it in to a Matrix object
      String file = "C:/opencv/logo.jpg";
      Mat src="/?originalUrl=https%3A%2F%2Fwww.tutorialspoint.com%2FImgcodecs.imread(file)%3B%2520%2520%2520%2520%2520%2520%2F%2FCreating%2520an%2520empty%2520matrix%2520to%2520store%2520the%2520result%2520%2520%2520%2520%2520%2520Mat%2520dst%2520%3D%2520new%2520Mat()%3B%2520%2520%2520%2520%2520%2520%2F%2FScaling%2520the%2520Image%2520%2520%2520%2520%2520%2520Imgproc.resize(src%2C%2520dst%2C%2520new%2520Size(src.rows()%2F3%2C%2520src.rows()%2F3)%2C%25200%2C%25200%2C%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520Imgproc.INTER_AREA)%3B%2520%2520%2520%2520%2520%2520%2F%2FWriting%2520the%2520image%2520%2520%2520%2520%2520%2520Imgcodecs.imwrite("C:/opencv/scale_output.jpg", dst);
      System.out.println("Image Processed");
   }
}

Input

OpenCV Copy Input

Output

Scale Output
java_opencv
Advertisements