Search in sources :

Example 1 with RawBitmap

use of org.ebookdroid.common.bitmaps.RawBitmap in project LibreraReader by foobnix.

the class ImageExtractor method proccessOtherPage.

public Bitmap proccessOtherPage(PageUrl pageUrl, FileMeta meta) {
    int page = pageUrl.getPage();
    String path = pageUrl.getPath();
    boolean isNeedDisableMagicInPDFDjvu = false;
    LOG.d("Page Number", pageUrl.getPage());
    if (pageUrl.getPage() == COVER_PAGE || pageUrl.getPage() == COVER_PAGE_NO_EFFECT || pageUrl.getPage() == COVER_PAGE_WITH_EFFECT) {
        isNeedDisableMagicInPDFDjvu = true;
    }
    if (page < 0) {
        page = 0;
    }
    if (pageUrl.isCrop()) {
    // isNeedDisableMagicInPDFDjvu = true;
    }
    CodecDocument codeCache = null;
    if (isNeedDisableMagicInPDFDjvu) {
        codeCache = singleCodecContext(path, "", pageUrl.getWidth(), pageUrl.getHeight());
        if (meta != null && codeCache != null && FileMetaCore.isNeedToExtractPDFMeta(path)) {
            String bookAuthor = codeCache.getBookAuthor();
            if (TxtUtils.isNotEmpty(bookAuthor)) {
                meta.setAuthor(bookAuthor);
            }
            String bookTitle = codeCache.getBookTitle();
            if (TxtUtils.isNotEmpty(bookTitle)) {
                meta.setTitle(bookTitle);
            }
            LOG.d("PDF getBookAuthor", bookAuthor, bookTitle);
        }
    } else {
        codeCache = getNewCodecContext(path, "", pageUrl.getWidth(), pageUrl.getHeight());
    }
    if (codeCache == null) {
        LOG.d("TEST", "codecDocument == null" + path);
        return null;
    }
    if (isNeedDisableMagicInPDFDjvu && meta != null) {
        meta.setPages(codeCache.getPageCount());
    }
    final CodecPageInfo pageInfo = codeCache.getPageInfo(page);
    Bitmap bitmap = null;
    RectF rectF = new RectF(0, 0, 1f, 1f);
    final float k = (float) pageInfo.height / pageInfo.width;
    int width = pageUrl.getWidth();
    int height = (int) (width * k);
    LOG.d("Bitmap", width, height);
    LOG.d("Bitmap pageInfo.height", pageInfo.width, pageInfo.height);
    BitmapRef bitmapRef = null;
    CodecPage pageCodec = codeCache.getPage(page);
    if (pageUrl.getNumber() == 0) {
        rectF = new RectF(0, 0, 1f, 1f);
        if (isNeedDisableMagicInPDFDjvu) {
            bitmapRef = pageCodec.renderBitmapSimple(width, height, rectF);
        } else {
            bitmapRef = pageCodec.renderBitmap(width, height, rectF);
        }
        bitmap = bitmapRef.getBitmap();
        if (pageUrl.isCrop()) {
            if (BookType.DJVU.is(pageUrl.getPath())) {
                Bitmap sample = pageCodec.renderBitmapSimple(PageCropper.MAX_WIDTH, PageCropper.MAX_HEIGHT, rectF).getBitmap();
                bitmap = cropBitmap(bitmap, sample);
                sample.recycle();
                sample = null;
            } else {
                bitmap = cropBitmap(bitmap, bitmap);
            }
        }
    } else if (pageUrl.getNumber() == 1) {
        float right = (float) pageUrl.getCutp() / 100;
        rectF = new RectF(0, 0, right, 1f);
        bitmapRef = pageCodec.renderBitmap((int) (width * right), height, rectF);
        bitmap = bitmapRef.getBitmap();
        if (pageUrl.isCrop()) {
            bitmap = cropBitmap(bitmap, bitmap);
        }
    } else if (pageUrl.getNumber() == 2) {
        float right = (float) pageUrl.getCutp() / 100;
        rectF = new RectF(right, 0, 1f, 1f);
        bitmapRef = pageCodec.renderBitmap((int) (width * (1 - right)), height, rectF);
        bitmap = bitmapRef.getBitmap();
        if (pageUrl.isCrop()) {
            bitmap = cropBitmap(bitmap, bitmap);
        }
    }
    if (pageUrl.isInvert()) {
        final RawBitmap bmp = new RawBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()));
        bmp.invert();
        bitmap.recycle();
        bitmap = bmp.toBitmap().getBitmap();
    }
    if (pageUrl.getRotate() > 0) {
        final Matrix matrix = new Matrix();
        matrix.postRotate(pageUrl.getRotate());
        final Bitmap bitmap1 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        bitmap.recycle();
        bitmap = bitmap1;
    }
    if (pageUrl.isDoText() && !pageCodec.isRecycled()) {
        PageImageState.get().pagesText.put(pageUrl.getPage(), pageCodec.getText());
        PageImageState.get().pagesLinks.put(pageUrl.getPage(), pageCodec.getPageLinks());
    }
    if (!pageCodec.isRecycled()) {
        pageCodec.recycle();
    }
    if (isNeedDisableMagicInPDFDjvu) {
        codeCache.recycle();
    }
    if (!isNeedDisableMagicInPDFDjvu && MagicHelper.isNeedBookBackgroundImage()) {
        bitmap = MagicHelper.updateWithBackground(bitmap);
    }
    return bitmap;
}
Also used : Rect(android.graphics.Rect) CodecPage(org.ebookdroid.core.codec.CodecPage) RectF(android.graphics.RectF) RawBitmap(org.ebookdroid.common.bitmaps.RawBitmap) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) CodecPageInfo(org.ebookdroid.core.codec.CodecPageInfo) RawBitmap(org.ebookdroid.common.bitmaps.RawBitmap) BitmapRef(org.ebookdroid.common.bitmaps.BitmapRef) CodecDocument(org.ebookdroid.core.codec.CodecDocument)

Aggregations

Bitmap (android.graphics.Bitmap)1 Matrix (android.graphics.Matrix)1 Rect (android.graphics.Rect)1 RectF (android.graphics.RectF)1 BitmapRef (org.ebookdroid.common.bitmaps.BitmapRef)1 RawBitmap (org.ebookdroid.common.bitmaps.RawBitmap)1 CodecDocument (org.ebookdroid.core.codec.CodecDocument)1 CodecPage (org.ebookdroid.core.codec.CodecPage)1 CodecPageInfo (org.ebookdroid.core.codec.CodecPageInfo)1