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;
}
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();
}
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();
}
}
}
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();
}
}
}
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;
}
Aggregations