Skip to content

Commit dd9fb19

Browse files
Update b52_BufferedImageOpExample.java
1 parent de15449 commit dd9fb19

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,67 @@
1+
package a7_java.awt;
12

3+
import java.awt.*;
4+
import java.awt.geom.*;
5+
import java.awt.image.*;
6+
import java.io.File;
7+
import javax.imageio.ImageIO;
8+
9+
/* Educational example demonstrating BufferedImageOp interface methods */
10+
public class b52_BufferedImageOpExample {
11+
12+
public static void main(String[] args) {
13+
try {
14+
// Load source image
15+
File inputFile = new File("C:\\Users\\Pc\\OneDrive\\Masaüstü\\Forest.jpeg");
16+
BufferedImage sourceImage = ImageIO.read(inputFile);
17+
System.out.println("Source image loaded successfully!");
18+
19+
// DEMO 1: Image Filtering with ConvolveOp (Method 2: filter)
20+
// Creating a blur effect using 3x3 kernel
21+
float[] blurKernel = {
22+
1/9f, 1/9f, 1/9f,
23+
1/9f, 1/9f, 1/9f,
24+
1/9f, 1/9f, 1/9f
25+
};
26+
BufferedImageOp blurOperation = new ConvolveOp(new Kernel(3, 3, blurKernel));
27+
BufferedImage blurredImage = blurOperation.filter(sourceImage, null);
28+
System.out.println("Method 2 (filter): Blur effect applied successfully");
29+
30+
// DEMO 2: Creating Compatible Destination Image (Method 1: createCompatibleDestImage)
31+
// This method ensures the destination image is compatible with the source
32+
ColorModel cm = sourceImage.getColorModel();
33+
BufferedImage compatibleImage = blurOperation.createCompatibleDestImage(sourceImage, cm);
34+
System.out.println("Method 1 (createCompatibleDestImage): Compatible image created");
35+
36+
// DEMO 3: Getting Bounding Box (Method 3: getBounds2D)
37+
// Useful for determining the dimensions of the processed image
38+
Rectangle2D bounds = blurOperation.getBounds2D(sourceImage);
39+
System.out.println("Method 3 (getBounds2D): Image bounds - " +
40+
"Width: " + bounds.getWidth() +
41+
", Height: " + bounds.getHeight());
42+
43+
// DEMO 4: Point Transformation (Method 4: getPoint2D)
44+
// Demonstrates how points are mapped between source and destination
45+
Point2D sourcePoint = new Point2D.Float(100, 100);
46+
Point2D destinationPoint = blurOperation.getPoint2D(sourcePoint, null);
47+
System.out.println("Method 4 (getPoint2D): Point mapping - " +
48+
"Source: " + sourcePoint +
49+
" -> Destination: " + destinationPoint);
50+
51+
// DEMO 5: Rendering Hints (Method 5: getRenderingHints)
52+
// Get rendering hints used by the operation
53+
RenderingHints hints = blurOperation.getRenderingHints();
54+
System.out.println("Method 5 (getRenderingHints): " +
55+
(hints != null ? "Hints available" : "No specific hints"));
56+
57+
// Save processed images
58+
ImageIO.write(blurredImage, "jpg", new File("blurred_output.jpg"));
59+
ImageIO.write(compatibleImage, "jpg", new File("compatible_output.jpg"));
60+
System.out.println("Processing completed! Check output files.");
61+
62+
} catch (Exception e) {
63+
System.err.println("Error occurred: " + e.getMessage());
64+
e.printStackTrace();
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)