Web服务器通过接口 讲PDF转化为图片,并保存在目录下,Android 可以直接访问web目录下图片内容
/需要添加引用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();
}
}