Android 从WebServer 获取PDF转图片
2015-12-28 14:46
344 查看
Web服务器通过接口
讲PDF转化为图片,并保存在目录下,Android
可以直接访问web目录下图片内容
参考:android从网络中获得一张图片,并显示在屏幕上
c#或ASP.NET把PDF轉換為圖片源碼
/////需要添加引用Ghostscript.NET.dll,另一个dll放入bin内(需要两个dll Ghostscript.NET.dll,gsdll32.dll)
ASP.NET WebServer程序
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();
}
}
讲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 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();
}
}
相关文章推荐
- java-WEB中的监听器Lisener
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件