使用java.awt.image(
javadoc)和javax.imageio(
javadoc)API,您可以轻松地遍历图像的像素并自己执行像素化.
示例代码如下.您至少需要这些导入:javax.imageio.ImageIO,java.awt.image.BufferedImage,java.awt.image.Raster,java.awt.image.WritableRaster和java.io.File.
例:
// How big should the pixelations be?
final int PIX_SIZE = 10;
// Read the file as an Image
img = ImageIO.read(new File("image.jpg"));
// Get the raster data (array of pixels)
Raster src = img.getData();
// Create an identically-sized output raster
WritableRaster dest = src.createCompatibleWritableRaster();
// Loop through every PIX_SIZE pixels, in both x and y directions
for(int y = 0; y < src.getHeight(); y += PIX_SIZE) {
for(int x = 0; x < src.getWidth(); x += PIX_SIZE) {
// Copy the pixel
double[] pixel &