我正在尝试将pdf页面转换为图像。 我使用的是PdfViewer.jar文件。
听到我的代码我想将转换后的图像显示为imageview
package com.info.org;
import java.io.File;
import java.io.FileInputStream;
import net.sf.andpdf.nio.ByteBuffer;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Environment;
import android.util.Base64;
import android.widget.ImageView;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
public class PDFImageReadActivity extends Activity {
/** Called when the activity is first created. */
ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
byte[] bytes;
setContentView(R.layout.main);
imageView=(ImageView) findViewById(R.id.img);
File file = new File(Environment.getExternalStorageDirectory()+"/infobooks/Book5.pdf");
if (file.exists()) {
FileInputStream is = new FileInputStream(file);
long length = file.length();
bytes = new byte[(int) length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead = is.read(bytes, offset, bytes.length
- offset)) >= 0) {
offset += numRead;
}
ByteBuffer buffer = ByteBuffer.NEW(bytes);
String data = Base64.encodeToString(bytes, Base64.DEFAULT);
PDFFile pdf_file = new PDFFile(buffer);
PDFPage page = pdf_file.getPage(1);
RectF rect = new RectF(0, 0, (int) page.getBBox().width(),(int) page.getBBox().height());
Bitmap image = page.getImage((int) rect.width(),(int) rect.height(), rect);
imageView.setImageBitmap(image);
//FileOutputStream os = new FileOutputStream(Environment.getExternalStorageDirectory()+ "/pdfImage.jpg");
//image.compress(Bitmap.CompressFormat.JPEG, 80, os);
} else {
System.err.println("File is not found" + file.getAbsolutePath());
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("Error in main -->>" + e.toString());
}
}
}
当我运行它时会在logcat中显示错误
06-28 18:12:40.353: E/APV.PDFRenderer(852): do not show images
06-28 18:12:40.353: E/APV.PDFRenderer(852): java.lang.UnsupportedOperationException: do not show images
06-28 18:12:40.353: E/APV.PDFRenderer(852): at com.sun.pdfview.PDFImage.getImage(PDFImage.java:223)
06-28 18:12:40.353: E/APV.PDFRenderer(852): at com.sun.pdfview.PDFRenderer.drawImage(PDFRenderer.java:374)
06-28 18:12:40.353: E/APV.PDFRenderer(852): at com.sun.pdfview.PDFImageCmd.execute(PDFPage.java:654)
06-28 18:12:40.353: E/APV.PDFRenderer(852): at com.sun.pdfview.PDFRenderer.iterate(PDFRenderer.java:674)
06-28 18:12:40.353: E/APV.PDFRenderer(852): at com.sun.pdfview.BaseWatchable.run(BaseWatchable.java:101)
06-28 18:12:40.353: E/APV.PDFRenderer(852): at java.lang.Thread.run(Thread.java:1096)
任何人都可以看到问题吗?
此代码来自此帖子。 Need help to convert a Pdf page into Bitmap in Android Java
答案 0 :(得分:0)
您可以使用GIMP。它是一个非常好的界面。
将pdf文件导入GIMP 将文件另存为.xcf 在图像上操作 - 如果需要,编辑 以您想要的任何格式导出:.bmp,.jpeg,.png等
在这篇文章中回答:PDF to Image convertor