Skip to content

Commit

Permalink
Fixed orientation metadata detection and added unit tests per #783
Browse files Browse the repository at this point in the history
  • Loading branch information
Balearica committed Jun 21, 2023
1 parent 42c8b36 commit 233d098
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/worker-script/utils/setImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = (TessModule, api, image, angle = 0) => {
// Check for bmp magic numbers (42 and 4D in hex)
const isBmp = (image[0] === 66 && image[1] === 77) || (image[1] === 66 && image[0] === 77);

const exif = image.slice(0, 500).toString().match(/\x01\x12\x00\x03\x00\x00\x00\x01\x00(.)/)?.[1]?.charCodeAt(0) || 1;
const exif = parseInt(image.slice(0, 500).join(' ').match(/1 18 0 3 0 0 0 1 0 (\d)/)?.[1], 10) || 1;

// /*
// * Leptonica supports some but not all bmp files
Expand Down
Binary file added tests/assets/images/simple-180.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/assets/images/simple-270.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/assets/images/simple-90.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions tests/recognize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ describe('recognize()', () => {
));
});

describe('should support orientation metadata', () => {
[
{ name: 'simple-90.jpg', desc: 'simple', ans: SIMPLE_TEXT },
{ name: 'simple-180.jpg', desc: 'simple', ans: SIMPLE_TEXT },
{ name: 'simple-270.jpg', desc: 'simple', ans: SIMPLE_TEXT },
].forEach(({ name, desc, ans }) => (
it(`recongize ${desc} image`, async () => {
await worker.initialize('eng');
const { data: { text } } = await worker.recognize(`${IMAGE_PATH}/${name}`);
expect(text).to.be(ans);
}).timeout(TIMEOUT)
));
});

describe('should recognize base64 image (simplified interface)', () => {
[
{ format: 'png', image: SIMPLE_PNG_BASE64, ans: SIMPLE_TEXT },
Expand Down

0 comments on commit 233d098

Please sign in to comment.