Android 从WebServer 获取PDF转图片

本文介绍了一种将PDF文件转换为图片并通过Web服务器提供访问的方法。使用Ghostscript.NET库进行PDF到图片的转换,Android客户端可以通过HTTP请求获取这些图片。

Web服务器通过接口 PDF转化为图片,并保存在目录下,Android 可以直接访问web目录下图片内容

 

 

参考:android从网络中获得一张图片,并显示在屏幕上

c#ASP.NETPDF轉換為圖片源碼  

/////需要添加引用Ghostscript.NET.dll,另一个dll放入bin内(需要两个dll Ghostscript.NET.dll,gsdll32.dll)

 

 

 

ASP.NET WebServer程序

	 public int PDFToImage(string file, int dpi)
	        {
	            int i = 0;
	            foreach (string d in Directory.GetFileSystemEntries(System.Web.HttpContext.Current.Server.MapPath("\\" + "CntImg")))
	            {
	                if (File.Exists(d))
	                {
	                    FileInfo fi = new FileInfo(d);
	                    if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
	                        fi.Attributes = FileAttributes.Normal;
	                    File.Delete(d);//直接删除其中的文件 
	                }
	            }
	            string[] strimg;
	            string path = Path.GetDirectoryName(System.Web.HttpRuntime.BinDirectory);
	            Ghostscript.NET.Rasterizer.GhostscriptRasterizer rasterizer = null;
	            Ghostscript.NET.GhostscriptVersionInfo vesion = new Ghostscript.NET.GhostscriptVersionInfo(new System.Version(0, 0, 0), path + @"\gsdll32.dll", string.Empty, Ghostscript.NET.GhostscriptLicense.GPL);
	
	            using (rasterizer = new Ghostscript.NET.Rasterizer.GhostscriptRasterizer())
	            {
	                rasterizer.Open(file, vesion, false);
	                int count = rasterizer.PageCount;
	                strimg = new string[count];
	                for (i = 0; i < count; i++)
	                {
	                    string pageFilePath = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("\\" + "CntImg"), i.ToString() + ".jpg");
	                    if (File.Exists(pageFilePath))
	                    {
	                        File.Delete(pageFilePath);
	                    }
	                    System.Drawing.Image img = rasterizer.GetPage(dpi, dpi, i + 1);
	                    img.Save(pageFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);
	                   // strimg[i] = System.Convert.ToBase64String(Returnbyte(pageFilePath));
	                }
	                rasterizer.Close();
	            }
	            return i;
	        }
	        private byte[] Returnbyte(string strpath)
	        {
	            //以二进制方式读文件
	            FileStream fsMyfile = new FileStream(strpath, FileMode.OpenOrCreate, FileAccess.Read);
	            //创建一个二进制数据流读入器,和打开的文件关联
	            BinaryReader brMyfile = new BinaryReader(fsMyfile);
	            //把文件指针重新定位到文件的开始
	            brMyfile.BaseStream.Seek(0, SeekOrigin.Begin);
	            byte[] bytes = brMyfile.ReadBytes(Convert.ToInt32(fsMyfile.Length.ToString()));
	            //关闭以上的new的各个对象
	            brMyfile.Close();
	            return bytes;
	        }



Android端口代码



package com.bottle.stockmanage;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class GetImageStream {
	
	public static byte[] getImage(String path) throws IOException {  
        URL url = new URL(path);  
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
        conn.setRequestMethod("GET");   //设置请求方法为GET  
        conn.setReadTimeout(5*1000);    //设置请求过时时间为5秒  
        InputStream inputStream = conn.getInputStream();   //通过输入流获得图片数据  
        byte[] data = StreamTool.readInputStream(inputStream);//获得图片的二进制数据  
        return data;  
          
    }  

}

package com.bottle.stockmanage;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class StreamTool {
	 public static  byte[] readInputStream(InputStream inputStream) throws IOException {  
	        byte[] buffer = new byte[1024];  
	        int len = 0;  
	        ByteArrayOutputStream bos = new ByteArrayOutputStream();  
	        while((len = inputStream.read(buffer)) != -1) {  
	            bos.write(buffer, 0, len);  
	        }  
	        bos.close();  
	        return bos.toByteArray();  
	          
	    }  

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值