文件流转换工具、二维码生成工具、pdf转图片流
2017-02-20 15:29
786 查看
import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Random; import javax.imageio.ImageIO; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.PDFRenderer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; /** * 流转换工具 * 文件流转换工具 * 二维码转换工具 * @author syk * */ public class FileStreamConvertUtil { private static final Logger LOGGER = LoggerFactory.getLogger(FileStreamConvertUtil.class); /** * 获得指定文件的二进制数组 */ public static byte[] getBytesByFile(String filePath){ byte[] buffer = null; InputStream fis =null; ByteArrayOutputStream bos = null; try { fis = new ByteArrayInputStream(returnBitMap(filePath)); bos = new ByteArrayOutputStream(1000); byte[] b = new byte[1000]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } buffer = bos.toByteArray(); } catch (FileNotFoundException e) { LOGGER.error("FileNotFoundException e:{}",e.getLocalizedMessage()); } catch (IOException e) { LOGGER.error("IOException e:{}",e.getLocalizedMessage()); } finally { try { if (fis != null) { fis.close(); } } catch (IOException e) { LOGGER.error("FileInputStream close exception e:{}", e.getLocalizedMessage()); } try { if (bos != null) { bos.close(); } } catch (IOException e) { LOGGER.error("ByteArrayOutputStream close exception e:{}", e.getLocalizedMessage()); } } return buffer; } /** * 将pdf转换成图片二进制流 * http://blog.csdn.net/chenjhit/article/details/54410025 */ public static List<byte[]> pdfToImage(String filePath){ List<byte[]> imagesStream=new ArrayList<byte[]>(); List<byte[]> imagesStream=new ArrayList<byte[]>(); try { PDDocument doc = PDDocument.load(returnBitMap(filePath)); PDFRenderer renderer = new PDFRenderer(doc); int pageCount = doc.getNumberOfPages(); for(int i=0; i<pageCount; i++){ ByteArrayOutputStream os = null; ImageOutputStream ios = null; try{ BufferedImage image = renderer.renderImage(i, 1.5f);//scale=dpi/75f os = new ByteArrayOutputStream(); ImageWriter imageWriter=ImageIO.getImageWritersBySuffix("jpg").next(); ios = ImageIO.createImageOutputStream(os); imageWriter.setOutput(ios); IIOMetadata imageMetaData = imageWriter.getDefaultImageMetadata(new ImageTypeSpecifier(image), null); ImageWriteParam imageWriteParam =imageWriter.getDefaultWriteParam(); imageWriteParam.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT); imageWriteParam.setCompressionQuality(0.7f);//压缩率 imageWriter.write(imageMetaData,new IIOImage(image, null, null), imageWriteParam); imagesStream.add(os.toByteArray()); }finally{ if(os!=null){ os.close(); } if(ios!=null){ ios.close(); } } } } catch (Exception e) { LOGGER.error("pdf To Image exception e:{}",JosnLogUtil.formatEx(e)); } return imagesStream; } /** * 根据url获取InputStream * @param path * @return */ public static byte[] returnBitMap(String path) { URL url = null; InputStream is =null; HttpURLConnection conn=null; byte[] byteStream=null; try { url = new URL(path); } catch (MalformedURLException e) { LOGGER.error("return bit map url exception e:{}",e.getLocalizedMessage()); } try { conn = (HttpURLConnection) url.openConnection();//利用HttpURLConnection对象,可以从网络中获取网页数据. conn.setDoInput(true); conn.setConnectTimeout(30000); conn.connect(); is = conn.getInputStream(); //得到网络返回的输入流 byteStream=toByteArray(is); } catch (IOException e) { LOGGER.error("return bit map inputStream exception e:{}",e.getLocalizedMessage()); }finally{ try { if(conn!=null){ conn.disconnect(); } if(is!=null){ is.close(); } } catch (IOException e) { LOGGER.error("inputStream close exception e:{}",e.getLocalizedMessage()); } } return byteStream; } public static byte[] toByteArray(InputStream input){ byte[] outBytes=null; ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int n = 0; try { while (-1 != (n = input.read(buffer))) { output.write(buffer, 0, n); } outBytes=output.toByteArray(); } catch (IOException e) { LOGGER.error("to Byte Array exception e:{}",e.getLocalizedMessage()); }finally{ try { if(output!=null){ output.close(); } } catch (IOException e) { LOGGER.error("byteArrayOutputStream close exception e:{}",e.getLocalizedMessage()); } } return outBytes; } /** * 创建二维码 * @param _text * @return */ public static byte[] createQrcode(String _text) { byte[] imagesStream=null; try { int qrcodeWidth = 400; int qrcodeHeight = 400; String qrcodeFormat = "png"; HashMap<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.put(EncodeHintType.MARGIN, 0); BitMatrix bitMatrix = new MultiFormatWriter().encode(_text, BarcodeFormat.QR_CODE, qrcodeWidth, qrcodeHeight, hints); ByteArrayOutputStream os = null; try { os = new ByteArrayOutputStream(); MatrixToImageWriter.writeToStream(bitMatrix, qrcodeFormat, os); }catch(Exception e){ }finally{ if(os!=null){ os.close(); } } imagesStream=os.toByteArray(); } catch (Exception e) { LOGGER.error("create qrcode exception e:{}",e.getLocalizedMessage()); } return imagesStream; } }
相关文章推荐
- 二维码生成和pdf添加文件和图片
- 如何用不同的工具将图片转换成PDF文件
- pdf文件使用什么工具可转换成图片
- C# 把图片转换成字符串存入.txt文件中,并从.txt中读出生成图片
- 基于Aspose.Pdf把pdf文件每一页转换为图片
- 滴水穿石--Java 生成PDF文件--iText使用之插入图片和中文乱码
- 用IrfanView将图片文件转换为PDF文件(由图片创建PDF文件)
- 活用office工具把PDF文件转换成word文件
- ffmpeg 转换VC工具已经可以生成工程文件
- ffmpeg 转换VC工具 V1.1.2 支持android.mk工程文件生成
- ffmpeg 转换VC工具已经可以生成工程文件(续)
- winForm 读取pdf文件并,转换成图片
- ffmpeg 转换VC工具已经可以生成工程文件
- ffmpeg 转换VC工具已经可以生成工程文件
- ffmpeg 转换VC工具 V1.1.2 支持android.mk工程文件生成
- 自动运行xelatex命令两遍及xdvipdfmx生成pdf并删除临时文件的工具
- VBScript的PDF转换工具.6.辅助函数:配置文件读取
- VBScript的PDF转换工具.9.处理目录和单个文件
- ASP.NET上传视频文件同时转换为flv并且抓取第一帧生成图片
- 用IrfanView将图片文件转换为PDF文件(由图片创建PDF文件) .