Search This Blog

Monday, October 11, 2021

Java to Do ImageMagick Call

 package com.product.zilly;


import java.io.File;

import java.util.Map;


import org.slf4j.Logger;

import org.slf4j.LoggerFactory;


/**

 * @author drvijay

 * @description img class

 * @version 1.0

 */


@SuppressWarnings ("all")

public class TestModules implements Constants

{

private static final Logger logger = LoggerFactory.getLogger ( TestModules.class );


public static void main ( String args[] )

{

ImageHelper imageHelper = new ImageHelper ();

String renditionPath = "/home/drvijay/images"; 

String rendtionPathCategory = "/home/drvijay/images"; 

String startingMainPath = renditionPath + PATH_SEPARATOR + SkuRendition.main;

String startingCategoryMainPath = rendtionPathCategory + PATH_SEPARATOR + SkuRendition.main; // for category images main path

imageHelper.loadSkuImageProperties ();

boolean imageOverride = false; 

File rotated = null;

File file = new File ( startingMainPath + PATH_SEPARATOR + "sample.png" );

if ( file.isFile () && file.exists () )

{

Map <SkuRendition, Map <Resolution, DimInfo>> dimensions = imageHelper.getAllDimensions ();

logger.debug ( dimensions.toString () );

for ( SkuRendition skuRendition : dimensions.keySet () )

{

try

{

Map <Resolution, DimInfo> resolutions = dimensions.get ( skuRendition );

for ( Resolution resolution : resolutions.keySet () )

{

DimInfo dimInfo = resolutions.get ( resolution );


File isExist = new File ( renditionPath + PATH_SEPARATOR + skuRendition.name () + PATH_SEPARATOR + resolution.name () + UNDER_SCORE + file.getName () );

if ( ( imageOverride && isExist.isFile () && isExist.exists () ) || ( !isExist.isFile () && !isExist.exists () ) )

{

rotated = imageHelper.processImage ( file, imageHelper.getOutputDir ( renditionPath, skuRendition.name () ), dimInfo, resolution.name () );

logger.debug ( "Generated : " + rotated.getAbsolutePath () );

}


}

}

catch ( Exception e )

{

logger.error ( "Error : " + e );

}

finally

{


/*

* if ( rotated != null )

* {

* rotated.delete ();

* }

*/


}

}

}

}

}


//Iamge Process

public File processImage ( File inFile, File outputDir, DimInfo dimInfo, String resolution ) throws Exception

{

String fileName = resolution + UNDER_SCORE + inFile.getName ();

// String fileNameWithOutExt = FilenameUtils.removeExtension(fileName);

// String fileNameExt = FilenameUtils.getExtension ( fileName );


// check for windows and set the image magick home manually to avoid error.

String OS = System.getProperty ( "os.name" ).toLowerCase ();

if ( ( OS.indexOf ( "win" ) >= 0 ) )

{

// linux - "/usr/lib/mime/packages/imagemagick-6.q16";

String myPath = "C:/Program Files/ImageMagick-6.9.2-Q16"; // globalMaster.getSystemProperties ().get ( KEY_PATH_HOME_IMAGE_MAGICK ).getValue ();

ProcessStarter.setGlobalSearchPath ( myPath );

}

ConvertCmd convert = new ConvertCmd ();

IMOperation operation = new IMOperation ();

String input = inFile.getAbsolutePath ();

int tmbWidth = dimInfo.getWidth ();

int tmbHeight = dimInfo.getHeight ();


operation.addImage ( input );

operation.gravity ( LocalProperties.getPropertyValue ( "render.image.gravity" ) );

operation.thumbnail ( tmbHeight, tmbWidth );

operation.units ( LocalProperties.getPropertyValue ( "render.image.units" ) );

operation.density ( Integer.parseInt ( LocalProperties.getPropertyValue ( "render.image.density" ) ) );

operation.strip ();

operation.interlace ( LocalProperties.getPropertyValue ( "render.image.interlace" ) );

operation.gaussianBlur ( Double.parseDouble ( LocalProperties.getPropertyValue ( "render.image.gaussian.blur" ) ) );

operation.quality ( Double.parseDouble ( LocalProperties.getPropertyValue ( "render.image.quality" ) ) );


File retVal = new File ( outputDir, fileName );

operation.addImage ( retVal.getAbsolutePath () );

convert.run ( operation );


logger.debug ( "Preview file: " + retVal.getAbsolutePath () );

return retVal;

}


No comments:

Hit Counter


View My Stats