IM4Java教程: PDF 生成GIF

为什么80%的码农都做不了架构师?>>>   hot3.png

废话休叙,归入正题.

在服务器端需要安装GraphicsMagick,ghostscript.ImageMagick

 

		
			org.im4java
			im4java
			1.4.0
		


 



	private final static int DEFAULT_DPI = 72;
	//gif的宽度
	private static final int FIX_MAX_WIDTH = 600;
	

	/**
	 * 
	 * @description render single page pdf file to gif with gm command
	 * @param actualPageWidth: pdf宽度
	 * @param mpages:pdf内容
	 * @param interval : gif时间间隔
	 * @return
	 * @throws IOException
	 * @throws IM4JavaException
	 * @throws InterruptedException
	 */
	public byte[] renderImage(float actualPageWidth, byte[] mpages, int interval) throws IOException,
			IM4JavaException, InterruptedException {
		GMOperation op = new GMOperation();
		float scale = actualPageWidth / FIX_MAX_WIDTH;
		// 图片质量
		op.quality(75.0);
		op.addRawArgs("-density", Double.toString(DEFAULT_DPI / scale));
		op.addRawArgs("-gravity", "center");
		op.addRawArgs("-auto-orient");
		op.addRawArgs("-strip");
		op.addRawArgs("-delay", interval == 0 ? "50" : (interval / 10.0) + "");
		// 待处理图片的绝对路径
		op.addImage("-");
		//降低gif大小
		op.addRawArgs("-fuzz", "2%");
		op.addRawArgs("-layers", "Optimize");
		op.addImage("gif:-"); // output: stdout
		// 如果使用ImageMagick,设为false,使用GraphicsMagick,就设为true,默认为false
		ConvertCmd cmd = new ConvertCmd(false);
		//加缓存
		ByteArrayOutputStream baos = new ByteArrayOutputStream(65536);
		Pipe pipeInOut = new Pipe(new ByteArrayInputStream(mpages), baos);
		cmd.setInputProvider(pipeInOut);
		cmd.setOutputConsumer(pipeInOut);
		cmd.run(op);
		return baos.toByteArray();
	}

https://my.oschina.net/osgit/

转载于:https://my.oschina.net/osgit/blog/868894

你可能感兴趣的:(IM4Java教程: PDF 生成GIF)