I am writing my own Java code to work with http://www.mupdf.com/docs/example.c for my Android app.
I simply need to display single page pdf files without any extra options. So I did not use the MupdfCore.java, instead I modified the above .c code to work with JNI as following:
C code :
JNIEXPORT void JNICALL Java_com_example_loadpdf_MainActivity_render
(JNIEnv * pEnv, jobject pThis, jstring jfilename, jint jpagenumber, jint jzoom, jint jrotation)
{
const char *filename = (*pEnv)->GetStringUTFChars(pEnv,jfilename,0);
int pagenumber = (int)jpagenumber;
int zoom = (int)jzoom;
int rotation = (int)jrotation;
fz_context *ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
fz_register_document_handlers(ctx);
fz_document *doc = fz_open_document(ctx, filename);
int pagecount = fz_count_pages(doc);
fz_page *page = fz_load_page(doc, pagenumber - 1);
fz_matrix transform;
fz_rotate(&transform, rotation);
fz_pre_scale(&transform, zoom / 100.0f, zoom / 100.0f);
fz_rect bounds;
fz_bound_page(doc, page, &bounds);
fz_transform_rect(&bounds, &transform);
fz_irect bbox;
fz_round_rect(&bbox, &bounds);
fz_pixmap *pix = fz_new_pixmap_with_bbox(ctx, fz_device_rgb(ctx), &bbox);
fz_clear_pixmap_with_value(ctx, pix, 0xff);
fz_device *dev = fz_new_draw_device(ctx, pix);
fz_run_page(doc, page, dev, &transform, NULL);
fz_free_device(dev);
fz_write_png(ctx, pix, "/sdcard/data/out.png", 0);
fz_drop_pixmap(ctx, pix);
fz_free_page(doc, page);
fz_close_document(doc);
fz_free_context(ctx);
Java code:
package com.example.loadpdf;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
public class MainActivity extends Activity {
private native void render(String path, int pagenumber, int zoom, int rotation);
static{
System.loadLibrary("mupdf");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String path = Environment.getExternalStorageDirectory().toString() + "//data//test.pdf";
render(path,1,100,0);
}
The above code successfully converts the given pdf into a png file & stores it in the sdcard/data folder. But how can I return the image back to java code without writing it in the sdcard so that I can display it in an imageview ?
There is not much documentation regarding the methods in the mupdf library, so if anyone has any idea how I can achieve this, please let me know.