MagicImage 是一个 Java 函数库,可以用来访问不同格式的图片,也可以快速进行图片的相关操作,包括图片剪裁、缩放、锐利化、模糊化、色彩转换、压缩与交错化图片。
MagicImage 的 org.magiclen.magicimage 套件下有许多不同的类别,分别有着不同的用途:
- ImageBlurring:对图片进行模糊化处理。
- ImageBuffer:对文件或是图片进行缓冲处理。
- ImageColor:对图片的颜色进行处理。
- ImageExport:可输出图片至文件中。
- ImageMetadata:可从图片文件中取得图片的基本数据以及标签数据。
- ImageResize:对图片进行剪裁或是缩放处理。
- ImageSharpen:对图片进行锐利化处理。
- ImageInterlacer:对图片进行交错化处理。
取得 MagicImage
使用 MagicImage
以下例子都用 MagicCat.jpg 这张 3776x2520 的图片来举例,可以点我下载原图。
读取图片文件中的基本标签数据
final File imageFile = new File("/home/magiclen/MagicCat.jpg");
final JSONObject jsonImg = ImageMetadata.readMetadataFromFile(imageFile);
System.out.println(jsonImg.toString(true));
结果如下:
{
"Basic" : {
"Bits Per Pixel" : 24,
"Width DPI" : 300,
"Compression Algorithm" : "JPEG",
"Format Details" : "Jpeg/DCM",
"Width Inch" : 12.58666706085205,
"Height DPI" : 300,
"Format" : "JPEG",
"Height Inch" : 8.399999618530273,
"Height" : 2520,
"Mime Type" : "image/jpeg",
"Color Type" : "YCbCr",
"Width" : 3776,
"Number of Images" : 1
},
"Metadata" : [
{
"name" : "Make",
"id" : 271,
"type" : "String",
"value" : "Panasonic"
},
{
"name" : "Model",
"id" : 272,
"type" : "String",
"value" : "DMC-LX7"
},
{
"name" : "Orientation",
"id" : 274,
"type" : "Integer",
"value" : 1
},
{
"name" : "XResolution",
"id" : 282,
"type" : "Real",
"value" : 300.0
},
{
"name" : "YResolution",
"id" : 283,
"type" : "Real",
"value" : 300.0
},
{
"name" : "ResolutionUnit",
"id" : 296,
"type" : "Integer",
"value" : 2
},
{
"name" : "Software",
"id" : 305,
"type" : "String",
"value" : "Adobe Photoshop CC (Windows)"
},
{
"name" : "DateTime",
"id" : 306,
"type" : "String",
"value" : "2014:02:16 23:26:53"
},
{
"name" : "ExifOffset",
"id" : 34665,
"type" : "Integer",
"value" : 208
},
{
"name" : "ExposureTime",
"id" : 33434,
"type" : "Real",
"value" : 0.1
},
{
"name" : "FNumber",
"id" : 33437,
"type" : "Real",
"value" : 2.1
},
{
"name" : "ExposureProgram",
"id" : 34850,
"type" : "Integer",
"value" : 3
},
{
"name" : "PhotographicSensitivity",
"id" : 34855,
"type" : "Integer",
"value" : 200
},
{
"name" : "ExifVersion",
"id" : 36864,
"type" : "Byte Array",
"value" : [
48,
50,
51,
48
]
},
{
"name" : "DateTimeOriginal",
"id" : 36867,
"type" : "String",
"value" : "2014:02:16 17:47:32"
},
{
"name" : "DateTimeDigitized",
"id" : 36868,
"type" : "String",
"value" : "2014:02:16 17:47:32"
},
{
"name" : "ShutterSpeedValue",
"id" : 37377,
"type" : "Real",
"value" : 3.321928
},
{
"name" : "ApertureValue",
"id" : 37378,
"type" : "Real",
"value" : 2.140779
},
{
"name" : "ExposureCompensation",
"id" : 37380,
"type" : "Real",
"value" : 0.0
},
{
"name" : "MaxApertureValue",
"id" : 37381,
"type" : "Real",
"value" : 2.140625
},
{
"name" : "MeteringMode",
"id" : 37383,
"type" : "Integer",
"value" : 5
},
{
"name" : "LightSource",
"id" : 37384,
"type" : "Integer",
"value" : 0
},
{
"name" : "Flash",
"id" : 37385,
"type" : "Integer",
"value" : 16
},
{
"name" : "FocalLength",
"id" : 37386,
"type" : "Real",
"value" : 13.7
},
{
"name" : "ColorSpace",
"id" : 40961,
"type" : "Integer",
"value" : -1
},
{
"name" : "ExifImageWidth",
"id" : 40962,
"type" : "Integer",
"value" : 3776
},
{
"name" : "ExifImageLength",
"id" : 40963,
"type" : "Integer",
"value" : 2520
},
{
"name" : "FocalPlaneXResolution",
"id" : 41486,
"type" : "Real",
"value" : 4903.225799560547
},
{
"name" : "FocalPlaneYResolution",
"id" : 41487,
"type" : "Real",
"value" : 4903.225799560547
},
{
"name" : "FocalPlaneResolutionUnit",
"id" : 41488,
"type" : "Integer",
"value" : 3
},
{
"name" : "SensingMethod",
"id" : 41495,
"type" : "Integer",
"value" : 2
},
{
"name" : "FileSource",
"id" : 41728,
"type" : "Byte Array",
"value" : [
3
]
},
{
"name" : "SceneType",
"id" : 41729,
"type" : "Byte Array",
"value" : [
1
]
},
{
"name" : "CustomRendered",
"id" : 41985,
"type" : "Integer",
"value" : 0
},
{
"name" : "ExposureMode",
"id" : 41986,
"type" : "Integer",
"value" : 0
},
{
"name" : "WhiteBalance",
"id" : 41987,
"type" : "Integer",
"value" : 0
},
{
"name" : "DigitalZoomRatio",
"id" : 41988,
"type" : "Real",
"value" : 0.0
},
{
"name" : "FocalLengthIn35mmFormat",
"id" : 41989,
"type" : "Integer",
"value" : 70
},
{
"name" : "SceneCaptureType",
"id" : 41990,
"type" : "Integer",
"value" : 0
},
{
"name" : "GainControl",
"id" : 41991,
"type" : "Integer",
"value" : 1
},
{
"name" : "Contrast",
"id" : 41992,
"type" : "Integer",
"value" : 0
},
{
"name" : "Saturation",
"id" : 41993,
"type" : "Integer",
"value" : 0
},
{
"name" : "Sharpness",
"id" : 41994,
"type" : "Integer",
"value" : 0
},
{
"name" : "Compression",
"id" : 259,
"type" : "Integer",
"value" : 6
},
{
"name" : "XResolution",
"id" : 282,
"type" : "Real",
"value" : 72.0
},
{
"name" : "YResolution",
"id" : 283,
"type" : "Real",
"value" : 72.0
},
{
"name" : "ResolutionUnit",
"id" : 296,
"type" : "Integer",
"value" : 2
},
{
"name" : "JpgFromRawStart",
"id" : 513,
"type" : "Integer",
"value" : 838
},
{
"name" : "JpgFromRawLength",
"id" : 514,
"type" : "Integer",
"value" : 8060
}
]
}
读取图片文件
final File imageFile = new File("/home/magiclen/MagicCat.jpg");
final BufferedImage bi = ImageBuffer.getBufferedImages(imageFile)[0];
拷贝图片
final BufferedImage cloneBi = ImageBuffer.cloneBufferedImage(bi);
输出图片
final File outputFile = new File("/home/magiclen/MyCat.jpg");
System.out.println(ImageExport.exportToJPEG(bi, outputFile, 0.8f, true));
结果如下图:
剪裁图片
final BufferedImage result = ImageResize.crop(bi, 900, 900, 1250, 160);
final File outputFile = new File("/home/magiclen/MyCat-crop.jpg");
System.out.println(ImageExport.exportToJPEG(result, outputFile, 0.8f, true));
结果如下图:
锐化图片
final BufferedImage result = ImageSharpen.sharpen(bi, 3);
final File outputFile = new File("/home/magiclen/MyCat-sharpen.jpg");
System.out.println(ImageExport.exportToJPEG(result, outputFile, 0.8f, true));
结果如下图:
模糊化图片
final BufferedImage result = ImageBlurring.blur(bi, 20);
final File outputFile = new File("/home/magiclen/MyCat-blur.jpg");
System.out.println(ImageExport.exportToJPEG(result, outputFile, 0.8f, true));
final BufferedImage result2 = ImageBlurring.gaussianBlur(bi, 20);
final File outputFile2 = new File("/home/magiclen/MyCat-gaussian-blur.jpg");
System.out.println(ImageExport.exportToJPEG(result2, outputFile2, 0.8f, true));
一般模糊化结果如下图:
高斯模糊化结果如下图:
放大图片
final BufferedImage result = ImageResize.resize(bi, 4000, -1, false, false);
final File outputFile = new File("/home/magiclen/MyCat-4000.jpg");
System.out.println(ImageExport.exportToJPEG(result, outputFile, 0.8f, true));
结果如下图:
缩小图片
final BufferedImage result = ImageResize.shrink(bi, 1920, 720, -1); // Limit the width and height
final File outputFile = new File("/home/magiclen/MyCat-h720.jpg");
System.out.println(ImageExport.exportToJPEG(result, outputFile, 0.8f, true));
结果如下图:
灰阶化图片
final BufferedImage result = ImageColor.gray(bi);
final File outputFile = new File("/home/magiclen/MyCat-gray.png");
System.out.println(ImageExport.exportToPNG(result, outputFile, true));
结果如下图:
二值化图片
final BufferedImage result = ImageColor.binary(bi);
final File outputFile = new File("/home/magiclen/MyCat-binary.png");
System.out.println(ImageExport.exportToPNG(result, outputFile, true));
结果如下图:
负片化图片
final BufferedImage result = ImageColor.negative(bi);
final File outputFile = new File("/home/magiclen/MyCat-negative.jpg");
System.out.println(ImageExport.exportToJPEG(result, outputFile, 0.8f, true));
结果如下图:
交错化图片
为了使图片在网页中可以让访客拥有良好的浏览体验,可以使用交错的排列方式来保存图片。如此一来图片在浏览器中读取的时候,图片显示方式会由模糊到清晰,而不是从上到下循序读取。注意,这个功能需要 ImageMagick 可执行文件 magick 的支持。
ImageInterlacer.MAGICK_PATH = "/path/to/magick";
if (ImageInterlacer.isAvailable()) {
final boolean success = ImageInterlacer.setInterlace(file, ImageInterlacer.Interlace.PLANE, false);
final boolean interlace = ImageInterlacer.isInterlaced(file, false);
}










