assets bitmap 转_将Pdf页面转换为Android Java中的Bitmap

我解决了这个问题。它就像让设备有时间渲染每个页面一样简单。

要解决这个问题,你所要做的就是改变PDFPage page = pdf_file.getPage(2);

至PDFPage page = pdf_file.getPage(2, true);

首先,要在Android中查看PDF,您必须将PDF转换为图像,然后将其显示给用户。(我将使用webview)

所以要做到这一点,我们需要这个库。这是我编辑的这个git的版本。

将库导入项目后,需要创建活动。

XML:<?xml  version="1.0" encoding="utf-8"?>

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/webView1"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

java://Imports:import android.app.Activity;import android.app.ProgressDialog;import android.content.Intent;import android.graphics.Bitmap;import android.os.AsyncTask;import android.os.Bundle;import android.os.Environment;import android.util.Base64;import android.util.Log;import android.view.View;import android.view.ViewTreeObserver;import android.webkit.WebView;import com.sun.pdfview.PDFFile;import com.sun.pdfview.PDFImage;import com.sun.pdfview.PDFPage;import com.sun.pdfview.PDFPaint;import net.sf.andpdf.nio.ByteBuffer;import net.sf.andpdf.refs.HardReference;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.RandomAccessFile;import java.nio.channels.FileChannel;//Globals:private WebView wv;private int ViewSize = 0;//OnCreate Method:@Overrideprotected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//Settings

PDFImage.sShowImages = true; // show images

PDFPaint.s_doAntiAlias = true; // make text smooth

HardReference.sKeepCaches = true; // save images in cache

//Setup webview

wv = (WebView)findViewById(R.id.webView1);

wv.getSettings().setBuiltInZoomControls(true);//show zoom buttons

wv.getSettings().setSupportZoom(true);//allow zoom

//get the width of the webview

wv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()

{

@Override

public void onGlobalLayout()

{

ViewSize = wv.getWidth();

wv.getViewTreeObserver().removeGlobalOnLayoutListener(this);

}

});

try

{

File file = new File(Environment.getExternalStorageDirectory().getPath() + "/randompdf.pdf");

RandomAccessFile f = new RandomAccessFile(file, "r");

byte[] data = new byte[(int)f.length()];

f.readFully(data);

pdfLoadImages(data);

}

catch(Exception ignored)

{

}}//Load Images:private void pdfLoadImages(final byte[] data){

try

{

// run async

new AsyncTask()

{

// create and show a progress dialog

ProgressDialog progressDialog = ProgressDialog.show(MainActivity.this, "", "Opening...");

@Override

protected void onPostExecute(String html)

{

//after async close progress dialog

progressDialog.dismiss();

//load the html in the webview

wv.loadDataWithBaseURL("", html, "text/html","UTF-8", "");

}

@Override

protected String doInBackground(Void... params)

{

try

{

//create pdf document object from bytes

ByteBuffer bb = ByteBuffer.NEW(data);

PDFFile pdf = new PDFFile(bb);

//Get the first page from the pdf doc

PDFPage PDFpage = pdf.getPage(1, true);

//create a scaling value according to the WebView Width

final float scale = ViewSize / PDFpage.getWidth() * 0.95f;

//convert the page into a bitmap with a scaling value

Bitmap page = PDFpage.getImage((int)(PDFpage.getWidth() * scale), (int)(PDFpage.getHeight() * scale), null, true, true);

//save the bitmap to a byte array

ByteArrayOutputStream stream = new ByteArrayOutputStream();

page.compress(Bitmap.CompressFormat.PNG, 100, stream);

byte[] byteArray = stream.toByteArray();

stream.reset();

//convert the byte array to a base64 string

String base64 = Base64.encodeToString(byteArray, Base64.NO_WRAP);

//create the html + add the first image to the html

                    String html = "html>


";

//loop though the rest of the pages and repeat the above

for(int i = 2; i <= pdf.getNumPages(); i++)

{

PDFpage = pdf.getPage(i, true);

page = PDFpage.getImage((int)(PDFpage.getWidth() * scale), (int)(PDFpage.getHeight() * scale), null, true, true);

page.compress(Bitmap.CompressFormat.PNG, 100, stream);

byteArray = stream.toByteArray();

stream.reset();

base64 = Base64.encodeToString(byteArray, Base64.NO_WRAP);

                        html += "
";

}

stream.close();

html += "";

return html;

}

catch (Exception e)

{

Log.d("error", e.toString());

}

return null;

}

}.execute();

System.gc();// run GC

}

catch (Exception e)

{

Log.d("error", e.toString());

}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值