Skip to content

Commit

Permalink
[add] Camera component & OCR page
Browse files Browse the repository at this point in the history
  • Loading branch information
TechQuery committed Sep 28, 2024
1 parent 6e72849 commit 46b243d
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 23 deletions.
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default tsEslint.config(
noSortAlphabetically: true
}
],
'@typescript-eslint/no-explicit-any': 'warn',
'@typescript-eslint/no-empty-object-type': 'off',
'@typescript-eslint/no-unsafe-declaration-merging': 'warn'
}
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "PaperBookReader",
"name": "@open-source-bazaar/paper-book-reader",
"version": "0.4.0",
"description": "App Project scaffold of WebCell v3",
"keywords": [
Expand All @@ -23,6 +23,7 @@
"dom-renderer": "^2.3.0",
"iterable-observer": "^1.1.0",
"mobx": "^6.13.3",
"tesseract.js": "^5.1.1",
"web-cell": "^3.0.0",
"web-utility": "^4.4.0"
},
Expand All @@ -49,6 +50,7 @@
"postcss": "^8.4.47",
"prettier": "^3.3.3",
"prettier-plugin-css-order": "^2.1.2",
"process": "^0.11.10",
"typescript": "~5.6.2",
"typescript-eslint": "^8.7.0",
"workbox-cli": "^7.1.0"
Expand Down
102 changes: 102 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions src/component/Camera.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { component, observer, WebCell, WebCellProps } from 'web-cell';
import { stringifyCSS } from 'web-utility';

export interface CameraViewProps extends WebCellProps {
onCapture?: (event: CustomEvent<Blob>) => any;
}

export interface CameraView extends WebCell<CameraViewProps> {}

@component({ tagName: 'camera-view', mode: 'open' })
@observer
export class CameraView
extends HTMLElement
implements WebCell<CameraViewProps>
{
cssCode = stringifyCSS({
':host': { display: 'block' },
video: { width: '100%', height: '100%' }
});

async init(video?: HTMLVideoElement) {
const { width, height } = getComputedStyle(video);

const stream = await navigator.mediaDevices.getUserMedia({
video: { width: parseFloat(width), height: parseFloat(height) }
});

video.srcObject = stream;
video.play();
}

handleCapture = async (event: MouseEvent) => {
const video = event.currentTarget as HTMLVideoElement;

if (video.paused) return video.play();

video.pause();

const { width, height } = getComputedStyle(video);

const canvas = new OffscreenCanvas(
parseFloat(width),
parseFloat(height)
);
const context = canvas.getContext('2d');

context.drawImage(video, 0, 0);

const blob = await canvas.convertToBlob();

this.emit('capture', blob);
};

render() {
return (
<>
<style>{this.cssCode}</style>

<video ref={this.init} onClick={this.handleCapture} />
</>
);
}
}
Loading

0 comments on commit 46b243d

Please sign in to comment.