WebServer服务器PDF转图片,Android显示

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

 

 

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

c#或ASP.NET把PDF轉換為圖片源碼 

 

/需要添加引用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 inDirectory.GetFileSystemEntries(System.Web.HttpContext.Current.Server.MapPath("\\"+ "CntImg")))
            {
                if (File.Exists(d))
                {
                    FileInfo fi = newFileInfo(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 = newGhostscript.NET.GhostscriptVersionInfo(new System.Version(0, 0, 0), path +@"\gsdll32.dll", string.Empty, Ghostscript.NET.GhostscriptLicense.GPL);
 
            using (rasterizer = newGhostscript.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(stringstrpath)
        {
           //以二进制方式读文件
            FileStream fsMyfile = newFileStream(strpath, FileMode.OpenOrCreate, FileAccess.Read);
           //创建一个二进制数据流读入器,和打开的文件关联
            BinaryReader brMyfile = newBinaryReader(fsMyfile);
           //把文件指针重新定位到文件的开始
            brMyfile.BaseStream.Seek(0,SeekOrigin.Begin);
            byte[] bytes =brMyfile.ReadBytes(Convert.ToInt32(fsMyfile.Length.ToString()));
            //关闭以上的new的各个对象
            brMyfile.Close();
            return bytes;
        }

 

 

 

Android端口代码

packagecom.bottle.stockmanage;
 
importjava.io.IOException;
importjava.io.InputStream;
importjava.net.HttpURLConnection;
import java.net.URL;
 
public classGetImageStream {
 
publicstatic 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秒 
        InputStreaminputStream = conn.getInputStream();   //通过输入流获得图片数据 
        byte[] data =StreamTool.readInputStream(inputStream);//获得图片的二进制数据 
        return data; 
         
    } 
 
}
 
packagecom.bottle.stockmanage;
 
importjava.io.ByteArrayOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
 
public classStreamTool {
 public static byte[] readInputStream(InputStream inputStream) throws IOException{ 
        byte[] buffer = new byte[1024]; 
        int len = 0; 
        ByteArrayOutputStream bos = newByteArrayOutputStream(); 
        while((len = inputStream.read(buffer))!= -1) { 
            bos.write(buffer, 0, len); 
        } 
        bos.close(); 
        return bos.toByteArray(); 
         
    } 
 
}


 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值