diff --git a/src/core/ocr/buffer-size-trim.ts b/src/core/ocr/buffer-size-trim.ts index d60e0a84..62867d09 100644 --- a/src/core/ocr/buffer-size-trim.ts +++ b/src/core/ocr/buffer-size-trim.ts @@ -5,18 +5,30 @@ import { BreachProtocolBufferSizeFragmentResult } from './buffer-size'; export class BreachProtocolBufferSizeTrimFragment< TImage > extends BreachProtocolBufferSizeBase { - override readonly fragment = this.container.processBufferSizeFragment(this.boundingBox); + override readonly fragment = this.container.processBufferSizeFragment( + this.boundingBox + ); // Ensure compatibility with current api. async recognize( threshold?: number ): Promise { - const { buffer, width } = await this.container.trim(this.fragment); + const { buffer, width } = await this.trimFragment(); const bufferSize = await this.getBufferSizeFromPixels(width); return this.getFragmentResult(null, bufferSize, buffer, null); } + private async trimFragment() { + try { + return await this.container.trim(this.fragment); + } catch (e) { + const buffer = await this.container.toBuffer(this.fragment); + + return { buffer, width: 0 }; + } + } + private async getBufferSizeFromPixels(width: number) { const { innerWidth } = this.boundingBox; diff --git a/src/core/ocr/buffer-size.ts b/src/core/ocr/buffer-size.ts index 4f405ae1..807f1a49 100644 --- a/src/core/ocr/buffer-size.ts +++ b/src/core/ocr/buffer-size.ts @@ -82,7 +82,7 @@ export class BreachProtocolBufferSizeFragment< // Second control group has some white pixels, threshold is too low. start = m + 1; } - } while (i++ < Math.log2(base) + 1); + } while (++i < Math.log2(base)); // No threshold found. return base;