package com.docus.demo.utils ;
import com.docus.demo.LoadSaveDemo ;
import com.docus.demo.entity.DemoData ;
import leadtools.* ;
import leadtools.codecs.CodecsLoadByteOrder ;
import leadtools.codecs.CodecsSavePageMode ;
import leadtools.codecs.RasterCodecs ;
import leadtools.demos.DemoUtilities ;
import lombok.extern.slf4j.Slf4j ;
import javax.imageio.ImageIO ;
import javax.imageio.ImageReader ;
import javax.imageio.stream.ImageInputStream ;
import java.awt.* ;
import java.awt.geom.AffineTransform ;
import java.awt.image.BufferedImage ;
import java.io.* ;
@Slf4j
public class ImageUtils {
private static final ImageUtils instance = new ImageUtils ( ) ;
public static ImageUtils getInstance ( ) {
return instance ;
}
public boolean loadLibraries ( ) {
try {
Platform . setLibPath ( DemoUtilities . getLibPath ( ) ) ;
Platform . loadLibrary ( LTLibrary . LEADTOOLS ) ;
Platform . loadLibrary ( LTLibrary . CODECS ) ;
Platform . loadLibrary ( LTLibrary . SVG ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
return false ;
}
return true ;
}
// public static void main(String[] args) {
// ImageUtils imageUtils = new ImageUtils();
// imageUtils.savePic("C:\\Users\\Administrator\\Desktop\\QSL0015.jp2","C:\\Users\\Administrator\\Desktop\\QSL0015.jpg");
// }
private boolean parseCommandLine ( String [ ] args , DemoData demoData ) {
try {
// Process the command line
if ( args . length = = 0 | | args [ 0 ] . equals ( "?" ) ) {
return false ;
}
// Optional defaults
demoData . firstPage = 1 ;
demoData . lastPage = - 1 ;
demoData . outputBPP = - 1 ;
demoData . outputFormat = null ;
for ( int argIndex = 0 ; argIndex < args . length ; argIndex + = 2 ) {
String option = args [ argIndex ] ;
String data = ( argIndex < args . length - 1 ) ? args [ argIndex + 1 ] : "" ;
if ( option . equals ( "-i" ) ) {
demoData . inFilePath = data ;
} else if ( option . equals ( "-o" ) ) {
demoData . outFilePath = data ;
} else if ( option . equals ( "-f" ) ) {
demoData . outputFormat = LoadSaveDemo . OutputFormat . valueOf ( data . toUpperCase ( ) ) ;
} else if ( option . equals ( "-b" ) ) {
int value = Integer . parseInt ( data ) ;
demoData . outputBPP = value ;
} else if ( option . equals ( "-fp" ) ) {
int value = Integer . parseInt ( data ) ;
demoData . firstPage = value ;
} else if ( option . equals ( "-lp" ) ) {
int value = Integer . parseInt ( data ) ;
demoData . lastPage = value ;
} else {
System . out . println ( "Invalid option " + option ) ;
return false ;
}
}
} catch ( Exception ex ) {
System . out . println ( "Error " + ex . getMessage ( ) ) ;
return false ;
}
return true ;
}
private boolean isValidInputs ( DemoData demoData ) {
// Verify inFilePath
if ( DemoUtilities . isNullOrEmpty ( demoData . inFilePath ) ) {
System . out . println ( "inFilePath cannot be empty" ) ;
return false ;
}
// Verify outFilePath
if ( DemoUtilities . isNullOrEmpty ( demoData . outFilePath ) ) {
System . out . println ( "outFilePath cannot be empty" ) ;
return false ;
}
// Verify outputBPP
if ( demoData . outputBPP = = - 1 ) {
System . out . println ( "Invalid bits per pixel" ) ;
return false ;
}
// Verify outputFormat
if ( demoData . outputFormat = = null ) {
System . out . println ( "Invalid output format" ) ;
return false ;
}
return true ;
}
/ * *
* tif 文 件 转 jpg
* @param inPutFile 文 件 输 入
* @param outFile 文 件 输 出
* @return
* /
public boolean tifToJpg ( String inPutFile , String outFile ) {
try
{
if ( createParentRoot ( outFile ) ) {
return false ;
}
InputStream inputStream = new FileInputStream ( inPutFile ) ;
OutputStream outputStream = new FileOutputStream ( outFile ) ;
byte [ ] buffer = new byte [ 1024 ] ;
int bytesRead ;
while ( ( bytesRead = inputStream . read ( buffer ) ) ! = - 1 ) {
outputStream . write ( buffer , 0 , bytesRead ) ;
}
log . info ( inPutFile + "成功将TIFF文件的字节码写入到新文件 " + outFile ) ;
return true ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
return false ;
}
/ * *
* 旋 转 jpg 文 件
*
* @param inPutFile 文 件 输 入 路 径
* @param outFile 文 件 输 出 路 径
* @param degreesToRotate 需 要 旋 转 的 角 度 整 数 是 顺 时 针 负 数 是 逆 时 针
* @return
* /
public boolean rotateFile ( String inPutFile , String outFile , double degreesToRotate ) {
try {
if ( createParentRoot ( outFile ) ) {
return false ;
}
BufferedImage image = ImageIO . read ( new File ( inPutFile ) ) ;
// 创建旋转后的图像
AffineTransform transform = new AffineTransform ( ) ;
transform . rotate ( Math . toRadians ( degreesToRotate ) , image . getWidth ( ) / 2.0 , image . getHeight ( ) / 2.0 ) ;
BufferedImage rotatedImage = new BufferedImage ( image . getWidth ( ) , image . getHeight ( ) , image . getType ( ) ) ;
Graphics2D g = rotatedImage . createGraphics ( ) ;
g . drawImage ( image , transform , null ) ;
g . dispose ( ) ;
ImageIO . write ( rotatedImage , "jpg" , new File ( outFile ) ) ;
return true ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
return false ;
}
}
public boolean savePic ( String inputfile , String outFile ) {
DemoData demoData = new DemoData ( ) ;
demoData . firstPage = 1 ;
demoData . lastPage = 1 ;
demoData . outputBPP = 24 ;
demoData . inFilePath = inputfile ;
demoData . outFilePath = outFile ;
demoData . outputFormat = LoadSaveDemo . OutputFormat . JPEG ;
try {
if ( createParentRoot ( outFile ) ) {
return false ;
}
// Load LEADTOOLS libraries
if ( ! loadLibraries ( ) )
return false ;
// Set the license
if ( ! DemoUtilities . setLicense ( ) ) {
System . out . println ( "Please Set Your Runtime License...\nExiting Demo..." ) ;
return false ;
}
// Set the Shadow Fonts
DemoUtilities . setShadowFonts ( ) ;
// Show the values:
// System.out.println("inFilePath: " + demoData.inFilePath);
// System.out.println("outFilePath: " + demoData.outFilePath);
// System.out.println("outputFormat: " + demoData.outputFormat);
// System.out.println("outputBPP: " + demoData.outputBPP);
// System.out.println("firstPage: " + demoData.firstPage);
// System.out.println("lastPage: " + demoData.lastPage);
// Run the demo
RasterCodecs rasterCodecs = null ;
RasterImage rasterImage = null ;
try {
// Initiate raster codecs
rasterCodecs = new RasterCodecs ( ) ;
//Enable Loading Text Files
if ( demoData . inFilePath . toLowerCase ( ) . endsWith ( ".txt" ) )
rasterCodecs . getOptions ( ) . getTxt ( ) . getLoad ( ) . setEnabled ( true ) ;
// Create a LEAD stream from the input file
ILeadStream stream = LeadStreamFactory . create ( demoData . inFilePath ) ;
// Get the number of pages in the input file
int pageCount = rasterCodecs . getTotalPages ( stream ) ;
// Verify user first/last page values are in range
if ( demoData . firstPage = = 0 ) demoData . firstPage = 1 ;
if ( demoData . lastPage = = - 1 ) demoData . lastPage = pageCount ;
if ( demoData . firstPage < 1 )
throw new IllegalArgumentException ( String . format ( "firstPage must be greater than or equal to 1. Value is %1s" , demoData . firstPage ) ) ;
if ( demoData . lastPage > pageCount )
throw new IllegalArgumentException ( String . format ( "lastPage must be smaller than or equal to %1s. Value is %2s" , pageCount , demoData . lastPage ) ) ;
if ( demoData . firstPage > demoData . lastPage )
throw new IllegalArgumentException ( String . format ( "firstPage (value is %1s) cannot be greater than lastPage (value is %2s)" , demoData . firstPage , demoData . lastPage ) ) ;
// Load image
// System.out.println("Load image...");
rasterImage = rasterCodecs . load ( stream , 0 , CodecsLoadByteOrder . BGR_OR_GRAY , demoData . firstPage , demoData . lastPage ) ;
// Save image
// System.out.println("Save image...");
rasterCodecs . save ( rasterImage , demoData . outFilePath , RasterImageFormat . valueOf ( demoData . outputFormat . toString ( ) ) , demoData . outputBPP , 1 , - 1 , 1 , CodecsSavePageMode . OVERWRITE ) ;
log . info ( "Image saved successfully here: " + demoData . outFilePath ) ;
return true ;
} catch ( Exception e ) {
// System.out.println(e.getMessage());
return false ;
} finally {
// Dispose the raster codecs
if ( rasterCodecs ! = null )
rasterCodecs . dispose ( ) ;
// Dispose the raster image
if ( rasterImage ! = null )
rasterImage . dispose ( ) ;
}
} catch ( Exception ex ) {
log . info ( ex . getMessage ( ) ) ;
}
return false ;
}
private boolean createParentRoot ( String outFile ) {
//判断父级目录是否存在 不存在需要创建
File file = new File ( outFile ) ;
String parentPath = file . getParent ( ) ;
File dir = new File ( parentPath ) ;
if ( ! dir . exists ( ) ) {
boolean b = dir . mkdirs ( ) ;
if ( ! b ) {
System . out . println ( "created parent dir fail :" + outFile ) ;
return true ;
}
}
return false ;
}
// public static void main(String[] args) {
// ImageUtils imageUtils = new ImageUtils();
//// imageUtils.savePic("C:\\Users\\Administrator\\Desktop\\HNSET0001.jp2.jp2","C:\\Users\\Administrator\\Desktop\\HNSET0001.jp2.jpg");
// imageUtils.tifToJpg("C:\\Users\\Administrator\\Desktop\\HNSET0001.tif","C:\\Users\\Administrator\\Desktop\\HNSET0001.jpg");
// imageUtils.rotateFile("C:\\Users\\Administrator\\Desktop\\HNSET0001.jp2.jpg","C:\\Users\\Administrator\\Desktop\\HNSET0001.jp2.jpg",90);
// }
}