Esempio n. 1
0
 @Override
 public final void load(final String uri) throws ReaderException {
   super.load(uri);
   MyLog.i(TAG, "Loading URI" + uri);
   try {
     file = new PDF(new File(uri), 1);
   } catch (UnsatisfiedLinkError e) {
     MyLog.e(TAG, "PDF library not available");
     file = null;
   }
 }
Esempio n. 2
0
  @Override
  public final Drawable getPage(final int page) throws ReaderException {
    // check limits
    if (this.file == null || page < 0 || page >= this.file.getPageCount()) {
      return null;
    }

    ArrayList<Bitmap> tiles = new ArrayList<Bitmap>();

    int zoom = ZOOM100; // 1000 means 100%

    // Should we rotate the bitmaps?
    boolean rotate = false;
    PDF.Size size = new PDF.Size();
    file.getPageSize(page, size);

    int cols = 2;
    int rows = 2;

    // test if we have to rotate the screen
    if (AUTOMATIC_ROTATION && size.width > size.height) {
      rotate = true;
      cols = this.getSuitableCols(size.height);
      rows = this.getSuitableRows(size.width);
    } else {
      cols = this.getSuitableCols(size.width);
      rows = this.getSuitableRows(size.height);
    }
    MyLog.d(TAG, "Using cols, rows: " + cols + ", " + rows);

    // calculate an appropriate zoom level to fill the screen
    // this enhance the quality of the rendered page.
    if (AUTOMATIC_ZOOM && getWidth() != -1) {
      if (rotate) {
        if (size.height < getWidth()) {
          // calculate the zoom level
          zoom = ZOOM100 * getWidth() / size.height;
          // if the zoom changes, the pdf size changes accordingly
          size.width = (int) (1.0 * zoom * size.width / ZOOM100);
          size.height = (int) (1.0 * zoom * size.height / ZOOM100);
          MyLog.d(TAG, "Using zoom level of " + zoom);
        } else {
          MyLog.d(TAG, "PDF page larger than viewport");
        }
      } else {
        if (size.width < getWidth()) {
          // calculate the zoom level
          zoom = ZOOM100 * getWidth() / size.width;
          // if the zoom changes, the pdf size changes accordingly
          size.width = (int) (1.0 * zoom * size.width / ZOOM100);
          size.height = (int) (1.0 * zoom * size.height / ZOOM100);
          MyLog.d(TAG, "Using zoom level of " + zoom);
        } else {
          MyLog.d(TAG, "PDF page larger than viewport");
        }
      }
    } else {
      MyLog.w(TAG, "Viewport size not set or no automatic zoom");
    }

    // Get the closest width and height that divisible by cols and rows
    // note1: the last columns/rows of pixels in the image will be lost
    int ow, oh;
    if (rotate) {
      // if the image is rotated, width and height switch places
      oh = (size.width / rows) * rows;
      ow = (size.height / cols) * cols;
    } else {
      ow = (size.width / cols) * cols;
      oh = (size.height / rows) * rows;
    }

    // Get the final tiles width and height
    int tw = ow / cols;
    int th = oh / rows;

    for (int i = 0; i < rows; i++) {
      for (int j = 0; j < cols; j++) {
        if (rotate) {
          int left = th * i;
          int top = tw * (cols - j - 1);

          PDF.Size tilesize = new PDF.Size(th, tw);
          int[] pixels = file.renderPage(page, zoom, left, top, 0, false, tilesize);
          Bitmap b =
              Bitmap.createBitmap(pixels, tilesize.width, tilesize.height, Bitmap.Config.RGB_565);

          Matrix matrix = new Matrix();
          matrix.postRotate(90);
          tiles.add(Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true));
          b.recycle();
        } else {
          int left = tw * j;
          int top = th * i;

          PDF.Size tilesize = new PDF.Size(tw, th);
          int[] pixels = file.renderPage(page, zoom, left, top, 0, false, tilesize);
          Bitmap b =
              Bitmap.createBitmap(pixels, tilesize.width, tilesize.height, Bitmap.Config.RGB_565);
          tiles.add(b);
        }
      }
    }
    return new TiledDrawable(tiles, cols, rows);
  }