Package cn.lihongjie.image
Class FastImageUtils
- java.lang.Object
-
- cn.lihongjie.image.FastImageUtils
-
public class FastImageUtils extends java.lang.ObjectFast Image Compression Utilities with Cross-Platform Support This utility class handles automatic loading of native libraries for different operating systems and architectures. Supports: - Windows x64 (.dll) - Linux x64 (.so) - macOS Intel x64 (.dylib) - macOS Apple Silicon ARM64 (.dylib) Usage:// Compress an image with 70% quality byte[] imageData = Files.readAllBytes(Paths.get("image.jpg")); byte[] compressed = FastImageUtils.compress(imageData, 70);
-
-
Constructor Summary
Constructors Constructor Description FastImageUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static byte[]compress(byte[] imageBytes, int quality)Compress image data with automatic format detection and quality control This method automatically detects whether the input is PNG or JPEG format and applies the appropriate compression algorithm.static byte[]compressHigh(byte[] imageBytes)Compress image with high quality (quality = 90) Output format will be the same as input format.static byte[]compressJpegFast(byte[] imageBytes, int quality)Fast JPEG compression using Rust's native image library This method provides faster JPEG compression compared to the standard compress() method, but may produce slightly larger files.static byte[]compressJpegFastHigh(byte[] imageBytes)Fast JPEG compression with high quality (quality = 90)static byte[]compressJpegFastLow(byte[] imageBytes)Fast JPEG compression with low quality (quality = 30)static byte[]compressJpegFastMedium(byte[] imageBytes)Fast JPEG compression with medium quality (quality = 60)static byte[]compressLow(byte[] imageBytes)Compress image with low quality for maximum compression (quality = 30) Output format will be the same as input format.static byte[]compressMedium(byte[] imageBytes)Compress image with medium quality (quality = 60) Output format will be the same as input format.static java.lang.StringgetPlatformInfo()Get information about the current platform and loaded librarystatic byte[]rotate(byte[] imageBytes, int angle)Rotate image by specified angle This method rotates the input image by the specified angle (90, 180, or 270 degrees clockwise).static byte[]rotate180(byte[] imageBytes)Rotate image 180 degreesstatic byte[]rotate270(byte[] imageBytes)Rotate image 270 degrees clockwise (90 degrees counter-clockwise)static byte[]rotate90(byte[] imageBytes)Rotate image 90 degrees clockwisestatic booleantestLibrary()Test if the native library is working correctly
-
-
-
Method Detail
-
compress
public static byte[] compress(byte[] imageBytes, int quality)Compress image data with automatic format detection and quality control This method automatically detects whether the input is PNG or JPEG format and applies the appropriate compression algorithm. The output format will be the same as the input format (PNG input -> PNG output, JPEG input -> JPEG output).- Parameters:
imageBytes- Input image data as byte array (PNG or JPEG format)quality- Compression quality (0-100, where 0 is highest compression, 100 is best quality)- Returns:
- Compressed image data as byte array in the same format as input
- Throws:
java.lang.IllegalArgumentException- if quality is not in range 0-100 or data is emptyjava.lang.RuntimeException- if compression fails or image format is unsupported
-
compressHigh
public static byte[] compressHigh(byte[] imageBytes)
Compress image with high quality (quality = 90) Output format will be the same as input format.- Parameters:
imageBytes- Input image data (PNG or JPEG format)- Returns:
- Compressed image data in the same format as input
-
compressMedium
public static byte[] compressMedium(byte[] imageBytes)
Compress image with medium quality (quality = 60) Output format will be the same as input format.- Parameters:
imageBytes- Input image data (PNG or JPEG format)- Returns:
- Compressed image data in the same format as input
-
compressLow
public static byte[] compressLow(byte[] imageBytes)
Compress image with low quality for maximum compression (quality = 30) Output format will be the same as input format.- Parameters:
imageBytes- Input image data (PNG or JPEG format)- Returns:
- Compressed image data in the same format as input
-
compressJpegFast
public static byte[] compressJpegFast(byte[] imageBytes, int quality)Fast JPEG compression using Rust's native image library This method provides faster JPEG compression compared to the standard compress() method, but may produce slightly larger files. It uses Rust's native image library instead of mozjpeg. The input image will be converted to JPEG format regardless of the input format.- Parameters:
imageBytes- Input image data as byte array (any supported format)quality- Compression quality (0-100, where 0 is highest compression, 100 is best quality)- Returns:
- Compressed JPEG image data as byte array
- Throws:
java.lang.IllegalArgumentException- if quality is not in range 0-100 or data is emptyjava.lang.RuntimeException- if compression fails
-
compressJpegFastHigh
public static byte[] compressJpegFastHigh(byte[] imageBytes)
Fast JPEG compression with high quality (quality = 90)- Parameters:
imageBytes- Input image data- Returns:
- Compressed JPEG image data
-
compressJpegFastMedium
public static byte[] compressJpegFastMedium(byte[] imageBytes)
Fast JPEG compression with medium quality (quality = 60)- Parameters:
imageBytes- Input image data- Returns:
- Compressed JPEG image data
-
compressJpegFastLow
public static byte[] compressJpegFastLow(byte[] imageBytes)
Fast JPEG compression with low quality (quality = 30)- Parameters:
imageBytes- Input image data- Returns:
- Compressed JPEG image data
-
rotate
public static byte[] rotate(byte[] imageBytes, int angle)Rotate image by specified angle This method rotates the input image by the specified angle (90, 180, or 270 degrees clockwise). The output format will be the same as the input format (PNG input -> PNG output, JPEG input -> JPEG output).- Parameters:
imageBytes- Input image data as byte array (PNG, JPEG, WebP, GIF, or BMP format)angle- Rotation angle in degrees (must be 90, 180, or 270)- Returns:
- Rotated image data as byte array in the same format as input
- Throws:
java.lang.IllegalArgumentException- if angle is not 90, 180, or 270, or if data is emptyjava.lang.RuntimeException- if rotation fails or image format is unsupported
-
rotate90
public static byte[] rotate90(byte[] imageBytes)
Rotate image 90 degrees clockwise- Parameters:
imageBytes- Input image data- Returns:
- Rotated image data in the same format as input
-
rotate180
public static byte[] rotate180(byte[] imageBytes)
Rotate image 180 degrees- Parameters:
imageBytes- Input image data- Returns:
- Rotated image data in the same format as input
-
rotate270
public static byte[] rotate270(byte[] imageBytes)
Rotate image 270 degrees clockwise (90 degrees counter-clockwise)- Parameters:
imageBytes- Input image data- Returns:
- Rotated image data in the same format as input
-
getPlatformInfo
public static java.lang.String getPlatformInfo()
Get information about the current platform and loaded library- Returns:
- Platform information string
-
testLibrary
public static boolean testLibrary()
Test if the native library is working correctly- Returns:
- true if library is working, false otherwise
-
-