Search in sources :

Example 1 with CodecPage

use of org.ebookdroid.core.codec.CodecPage in project LibreraReader by foobnix.

the class MultyDocSearchDialog method searchInThePDF.

public static int searchInThePDF(String path, String text, final Handler update1) {
    try {
        text = text.toLowerCase(Locale.US);
        CodecContext ctx = BookType.getCodecContextByPath(path);
        CodecDocument openDocument = null;
        CacheZipUtils.cacheLock.lock();
        try {
            String zipPath = CacheZipUtils.extracIfNeed(path, CacheDir.ZipApp).unZipPath;
            openDocument = ctx.openDocument(zipPath, "");
            LOG.d("searchInThePDF", openDocument, zipPath);
        } finally {
            CacheZipUtils.cacheLock.unlock();
        }
        if (!Model.get().isSearcingRun) {
            openDocument.recycle();
            ctx.recycle();
            return -1;
        }
        int pageCount = openDocument.getPageCount();
        Model.get().currentPagesCount = pageCount;
        Model.get().currentDoc = new File(path).getName();
        LOG.d("searchInThePDF", "pageCount", Model.get().currentPagesCount, Model.get().currentDoc);
        int emptyCount = 0;
        for (int i = 0; i < pageCount; i++) {
            if (!Model.get().isSearcingRun) {
                openDocument.recycle();
                ctx.recycle();
                return -1;
            }
            CodecPage page = openDocument.getPage(i);
            TextWord[][] textPage = page.getText();
            LOG.d("searchInThePDF", "getText", textPage != null ? textPage.length : "null");
            if (textPage == null || textPage.length <= 1) {
                emptyCount++;
                if (emptyCount >= 5) {
                    LOG.d("searchInThePDF", "Page is empty", emptyCount);
                    break;
                }
            }
            List<TextWord> findText = Page.findText(text, textPage);
            page.recycle();
            page = null;
            Model.get().currentPage = i;
            update1.sendEmptyMessage(0);
            if (!findText.isEmpty()) {
                openDocument.recycle();
                ctx.recycle();
                findText = null;
                openDocument = null;
                ctx = null;
                return i;
            }
            findText = null;
        }
        openDocument.recycle();
        ctx.recycle();
        openDocument = null;
        ctx = null;
        System.gc();
    } catch (Exception e) {
        LOG.e(e);
    }
    return -1;
}
Also used : CodecContext(org.ebookdroid.core.codec.CodecContext) CodecPage(org.ebookdroid.core.codec.CodecPage) File(java.io.File) TextWord(org.ebookdroid.droids.mupdf.codec.TextWord) CodecDocument(org.ebookdroid.core.codec.CodecDocument)

Example 2 with CodecPage

use of org.ebookdroid.core.codec.CodecPage in project LibreraReader by foobnix.

the class DecodeServiceBase method searchText.

@Override
public void searchText(final String text, final Page[] pages, final ResultResponse<Integer> response, final Runnable finish) {
    Thread t = new Thread() {

        @Override
        public void run() {
            for (Page page : pages) {
                if (!TempHolder.isSeaching) {
                    response.onResultRecive(Integer.MAX_VALUE);
                    finish.run();
                    return;
                }
                if (page.index.docIndex > 1) {
                    response.onResultRecive(page.index.docIndex * -1);
                }
                if (isRecycled.get()) {
                    TempHolder.isSeaching = false;
                    return;
                }
                if (page.texts == null) {
                    final CodecPage page2 = codecDocument.getPage(page.index.docIndex);
                    page.texts = page2.getText();
                    if (!page2.isRecycled()) {
                        executor.addAny(new Task(0) {

                            @Override
                            public void run() {
                                page2.recycle();
                            }
                        });
                    }
                }
                page.selectedText = new ArrayList<TextWord>();
                List<TextWord> findText = page.findText(text);
                if (findText != null && !findText.isEmpty()) {
                    page.selectedText = findText;
                    response.onResultRecive(page.index.docIndex);
                    LOG.d("Find on page1", page.index.docIndex, text);
                }
            }
            response.onResultRecive(-1);
            finish.run();
            TempHolder.isSeaching = false;
        }
    };
    t.start();
}
Also used : CodecPage(org.ebookdroid.core.codec.CodecPage) CodecPage(org.ebookdroid.core.codec.CodecPage) TextWord(org.ebookdroid.droids.mupdf.codec.TextWord)

Example 3 with CodecPage

use of org.ebookdroid.core.codec.CodecPage in project LibreraReader by foobnix.

the class DecodeServiceBase method performDecode.

void performDecode(final DecodeTask task) {
    if (executor.isTaskDead(task)) {
        return;
    }
    CodecPageHolder holder = null;
    CodecPage vuPage = null;
    Rect r = null;
    RectF croppedPageBounds = null;
    // TempHolder.lock.lock();
    try {
        holder = getPageHolder(task.id, task.pageNumber);
        vuPage = holder.getPage(task.id);
        if (executor.isTaskDead(task)) {
            return;
        }
        croppedPageBounds = checkCropping(task, vuPage);
        if (executor.isTaskDead(task)) {
            return;
        }
        r = getScaledSize(task.node, task.viewState.zoom, croppedPageBounds, vuPage);
        final RectF actualSliceBounds = task.node.croppedBounds != null ? task.node.croppedBounds : task.node.pageSliceBounds;
        // TempHolder.lock.lock();
        final BitmapRef bitmap = vuPage.renderBitmap(r.width(), r.height(), actualSliceBounds);
        if (executor.isTaskDead(task)) {
            BitmapManager.release(bitmap);
            return;
        }
        // TempHolder.lock.lock();
        if (task.node.page.links == null) {
            task.node.page.links = vuPage.getPageLinks();
            if (LengthUtils.isNotEmpty(task.node.page.links)) {
            }
        }
        if (task.node.page.annotations == null) {
            task.node.page.annotations = vuPage.getAnnotations();
        }
        if (task.node.page.texts == null) {
            task.node.page.texts = vuPage.getText();
        }
        // TempHolder.lock.unlock();
        finishDecoding(task, vuPage, bitmap, r, croppedPageBounds);
    // test
    // vuPage.recycle();
    } catch (final OutOfMemoryError ex) {
        for (int i = 0; i <= AppSettings.getInstance().pagesInMemory; i++) {
            getPages().put(Integer.MAX_VALUE - i, null);
        }
        getPages().clear();
        if (vuPage != null) {
            vuPage.recycle();
        }
        BitmapManager.clear("DecodeService OutOfMemoryError: ");
        abortDecoding(task, null, null);
    } catch (final Throwable th) {
        th.printStackTrace();
        abortDecoding(task, vuPage, null);
    } finally {
        // TempHolder.lock.unlock();
        if (holder != null) {
            holder.unlock();
        }
    }
}
Also used : RectF(android.graphics.RectF) Rect(android.graphics.Rect) CodecPage(org.ebookdroid.core.codec.CodecPage) CodecPageHolder(org.ebookdroid.core.codec.CodecPageHolder) BitmapRef(org.ebookdroid.common.bitmaps.BitmapRef)

Example 4 with CodecPage

use of org.ebookdroid.core.codec.CodecPage in project LibreraReader by foobnix.

the class DecodeServiceBase method processTextForPages.

@Override
public void processTextForPages(Page[] pages) {
    for (Page page : pages) {
        if (isRecycled.get()) {
            return;
        }
        if (page.texts == null) {
            CodecPage page2 = codecDocument.getPage(page.index.docIndex);
            page.texts = page2.getText();
            page2.recycle();
        }
    }
}
Also used : CodecPage(org.ebookdroid.core.codec.CodecPage) CodecPage(org.ebookdroid.core.codec.CodecPage)

Example 5 with CodecPage

use of org.ebookdroid.core.codec.CodecPage 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

CodecPage (org.ebookdroid.core.codec.CodecPage)10 CodecDocument (org.ebookdroid.core.codec.CodecDocument)4 Rect (android.graphics.Rect)2 RectF (android.graphics.RectF)2 File (java.io.File)2 BitmapRef (org.ebookdroid.common.bitmaps.BitmapRef)2 TextWord (org.ebookdroid.droids.mupdf.codec.TextWord)2 TargetApi (android.annotation.TargetApi)1 Bitmap (android.graphics.Bitmap)1 Matrix (android.graphics.Matrix)1 OnUtteranceCompletedListener (android.speech.tts.TextToSpeech.OnUtteranceCompletedListener)1 UtteranceProgressListener (android.speech.tts.UtteranceProgressListener)1 FileMeta (com.foobnix.dao2.FileMeta)1 BufferedWriter (java.io.BufferedWriter)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 RawBitmap (org.ebookdroid.common.bitmaps.RawBitmap)1 CodecContext (org.ebookdroid.core.codec.CodecContext)1 CodecPageHolder (org.ebookdroid.core.codec.CodecPageHolder)1 CodecPageInfo (org.ebookdroid.core.codec.CodecPageInfo)1