Android下读取PDF文件的解决方案

代码修改自该处(开源 Android pdf 阅读器开发总结)作者分享到Google code的代码,经仔细阅读后整理出读取PDF文档的主要部分(vudroid so库的使用),原作者应该也是在vudroid项目下稍微进行了修改,代码有点乱,所以我整理了下,把关键地方抽了出来,项目中没有提供vudroid so库的源码,所以知道的接口只有那几个,想完整了解vudroid库接口的建议去下载vudroid官方源码,再自行通过ndk编译.(源码下载依然在最后)

Android下读取PDF文件的解决方案_第1张图片Android下读取PDF文件的解决方案_第2张图片

 

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.RectF;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.util.FloatMath;

import org.vudroid.pdfdroid.codec.*;


public class MainActivity extends Activity {
	private PdfContext pdf_conext;
	private ImageView view;
	
	private float screen_width = 800;
	private float screen_height = 1280;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        view = (ImageView)findViewById(R.id.imageView1);
        
        pdf_conext = new PdfContext();
        PdfDocument d = pdf_conext.openDocument("/sdcard/test.pdf");
        
        PdfPage vuPage = d.getPage(1);
        RectF rf = new RectF();
        rf.bottom = rf.right = (float)1.0;
        //参数一二为生成图片的宽高,参数三Rect的top,left,bottom,right为截取部分在该页中的百分比位置
        //例如top=0.5,left=0.5,right=1.0,1.0则截取右下角四分之一的部分
        Bitmap bitmap = vuPage.renderBitmap((int)screen_width, (int)screen_height, rf);
        view.setImageBitmap(bitmap);
    }

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
	
	Matrix matrix = new Matrix();
    Matrix savedMatrix = new Matrix();   
    static final int NONE = 0;   
    static final int DRAG = 1;   
    static final int ZOOM = 2;   
    int mode = NONE;   
    PointF start = new PointF();   
    PointF mid = new PointF();   
    float oldDist = 1f;   
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction() & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
			matrix.set(view.getImageMatrix());
			savedMatrix.set(matrix);
			start.set(event.getX(), event.getY());
			mode = DRAG;

			break;
		case MotionEvent.ACTION_POINTER_DOWN:
			oldDist = spacing(event);
			if (oldDist > 10f) {
				savedMatrix.set(matrix);
				midPoint(mid, event);
				mode = ZOOM;
			}
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_POINTER_UP:
			mode = NONE;
			break;
		case MotionEvent.ACTION_MOVE:
			if (mode == DRAG) {
				matrix.set(savedMatrix);
				matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
				
				keepInSight(matrix);
				
			} else if (mode == ZOOM) {
				float newDist = spacing(event);
				if (newDist > 10f) {
					matrix.set(savedMatrix);
					float scale = newDist / oldDist;
					matrix.postScale(scale, scale, mid.x, mid.y);
					
					keepInSight(matrix);
				}
			}
			break;
		}
		view.setImageMatrix(matrix);
		return true;
	}

	private float spacing(MotionEvent event) {
		float x = event.getX(0) - event.getX(1);
		float y = event.getY(0) - event.getY(1);
		return FloatMath.sqrt(x * x + y * y);
	}

	private void midPoint(PointF point, MotionEvent event) {
		float x = event.getX(0) + event.getX(1);
		float y = event.getY(0) + event.getY(1);
		point.set(x / 2, y / 2);
	}

	private void keepInSight(Matrix m){
		float f[] = new float[9];
		m.getValues(f);
		if(f[0]<1) f[0]=1;
		if(f[4]<1) f[4]=1;
		
		if(f[2]>0) f[2]=0;
		if(f[5]>0) f[5]=0;
		
		float l = screen_width-screen_width*f[0];
		if(f[2]<l) f[2] = l;
			
		float t = screen_height-screen_height*f[0];
		if(f[5]<t) f[5] = t;
		
		m.setValues(f);
	}
}

 

源码下载:

http://dl.vmall.com/c0x4dky4py

你可能感兴趣的:(pdf,andoird,vudroid)