0

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.

Shantanu Paul
  • 706
  • 10
  • 26
  • How about taking a look at how [MuPDFCore_drawPage()](http://git.ghostscript.com/?p=mupdf.git;a=blob;f=platform/android/jni/mupdf.c;h=ca13cc645094fe7bae5ba9235f3ac376c876ef7c;hb=HEAD#l663) does it? – Tamir Evan Jul 13 '14 at 17:08
  • ^^ No. I still couldn't understand how the whole function is implemented on the Java side. The java code is too complicated with 20+ .java files. – Shantanu Paul Jul 22 '14 at 19:42

1 Answers1

0

Is there any reason for you not to follow the already available android code?

You can just follow the guide in http://www.mupdf.com/docs/how-to-build-mupdf-for-android.

Simpler even you can follow this example: https://github.com/libreliodev/android

Gautam
  • 475
  • 1
  • 3
  • 8