Saturday, May 31, 2025

[JAVA] JAVA AUTO DRAWING IMAGE FROM IMAGE

import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class AutoDrawImage {

    public static void main(String[] args) {
        try {
            // Load the source image
            BufferedImage sourceImage = ImageIO.read(new File("path/to/your/source/image.jpg"));

            // Create a destination image
            BufferedImage destinationImage = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), BufferedImage.TYPE_INT_ARGB);

            // Get the graphics context
            Graphics2D g2d = destinationImage.createGraphics();

            // Draw the source image onto the destination
            g2d.drawImage(sourceImage, 0, 0, null);
            g2d.dispose();

            // Save the result
            ImageIO.write(destinationImage, "png", new File("path/to/your/output/image.png"));
            System.out.println("Image drawn and saved successfully!");

        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

No comments: