SaveIt.java Source code

Java tutorial

Introduction

Here is the source code for SaveIt.java

Source

import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class SaveIt {

    private static final float[] SHARP = { 0.0f, -1.0f, 0.0f, -1.0f, 5.0f, -1.0f, 0.0f, -1.0f, 0.0f };

    public static void main(String args[]) throws IOException {
        // Read
        File inputFile = new File("java2s.jpg");
        BufferedImage input = ImageIO.read(inputFile);
        // Convert
        Kernel kernel = new Kernel(3, 3, SHARP);
        ConvolveOp convolveOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
        int width = input.getWidth();
        int height = input.getHeight();
        BufferedImage output = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        convolveOp.filter(input, output);
        // Save
        File outputFile = new File("java2s.png");
        ImageIO.write(output, "PNG", outputFile);
    }
}