MagicImage是一個Java函式庫,可以用來存取不同格式的圖片,也可以快速進行圖片的相關操作,包括圖片剪裁、縮放、銳利化、模糊化、色彩轉換、壓縮與交錯化圖片。



MagicImage的org.magiclen.magicimage套件下有許多不同的類別,分別有著不同的用途:

  • ImageBlurring:對圖片進行模糊化處理。
  • ImageBuffer:對檔案或是圖片進行緩衝處理。
  • ImageColor:對圖片的顏色進行處理。
  • ImageExport:可輸出圖片至檔案中。
  • ImageMetadata:可從圖片檔案中取得圖片的基本資料以及標籤資料。
  • ImageResize:對圖片進行剪裁或是縮放處理。
  • ImageSharpen:對圖片進行銳利化處理。
  • ImageInterlacer:對圖片進行交錯化處理。

取得MagicImage

https://github.com/magiclen/MagicImage

使用MagicImage

以下例子都用「MagicCat.jpg」這張3776x2520的圖片來舉例,可用以下連結下載原圖:

https://file.magiclen.org/index.php?file=4d616769634361742e726172

讀取圖片檔案中的基本標籤資料

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));

結果如下圖:

magicimage

剪裁圖片

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));

結果如下圖:

magicimage

銳化圖片

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));

結果如下圖:

magicimage

模糊化圖片

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));

一般模糊化結果如下圖:

magicimage

高斯模糊化結果如下圖:

magicimage

放大圖片

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));

結果如下圖:

magicimage

縮小圖片

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));

結果如下圖:

magicimage

灰階化圖片

final BufferedImage result = ImageColor.gray(bi);
final File outputFile = new File("/home/magiclen/MyCat-gray.png");
System.out.println(ImageExport.exportToPNG(result, outputFile, true));

結果如下圖:

magicimage

二值化圖片

final BufferedImage result = ImageColor.binary(bi);
final File outputFile = new File("/home/magiclen/MyCat-binary.png");
System.out.println(ImageExport.exportToPNG(result, outputFile, true));

結果如下圖:

magicimage

負片化圖片

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));

結果如下圖:

magicimage

交錯化圖片

為了使圖片在網頁中可以讓訪客擁有良好的瀏覽體驗,可以使用交錯的排列方式來儲存圖片。如此一來圖片在瀏覽器中讀取的時候,圖片顯示方式會由模糊到清晰,而不是從上到下循序讀取。注意,這個功能需要「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);
}