Write me a html js css app that accept upload of an imagine an perform OCR
- User
Write me a html js css app that accept upload of an imagine an perform OCR
- Assistant
- User
All in on file please, full code
- Assistant
Write me a html js css app that accept upload of an imagine an perform OCR
All in on file please, full code
Creating an application that accepts an image upload and performs Optical Character Recognition (OCR) involves a few steps. Although doing OCR purely with JavaScript might not be as comprehensive as using server-side solutions or libraries, we can use a popular JavaScript library called Tesseract.js that performs OCR client-side using WebAssembly. Below is a simple example of how you can create such an application using HTML, CSS, and JavaScript with Tesseract.js:
Assume your project has the following structure:
/project
/index.html
/style.css
/app.js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OCR Image Upload</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>Image to OCR Text</h1>
<input type="file" id="imageUpload" accept="image/*" />
<div id="imagePreview"></div>
<button id="processButton">Start OCR</button>
<div id="ocrOutput"></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/tesseract.js@2/dist/tesseract.min.js"></script>
<script src="app.js"></script>
</body>
</html>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f7f8fa;
}
.container {
text-align: center;
max-width: 500px;
padding: 20px;
background: #fff;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}
#imagePreview {
margin-top: 15px;
max-height: 200px;
overflow: hidden;
}
#imagePreview img {
max-width: 100%;
border-radius: 8px;
}
#ocrOutput {
margin-top: 20px;
padding: 10px;
background: #e9ecef;
border-radius: 8px;
}
document.getElementById('imageUpload').addEventListener('change', handleImageUpload);
document.getElementById('processButton').addEventListener('click', performOCR);
let selectedImage = null;
function handleImageUpload(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
document.getElementById('imagePreview').innerHTML = `<img src="${e.target.result}" alt="Image Preview"/>`;
selectedImage = e.target.result;
};
reader.readAsDataURL(file);
}
}
function performOCR() {
if (!selectedImage) {
return alert('Please upload an image first');
}
Tesseract.recognize(
selectedImage,
'eng',
{
logger: info => console.log(info)
}
).then(({ data: { text } }) => {
document.getElementById('ocrOutput').textContent = text;
}).catch(err => {
console.error(err);
alert('An error occurred during OCR processing');
});
}
index.html, style.css, app.js) are in the same directory.index.html in a web browser.This simple app uses Tesseract.js, which might not perform as robustly as server-side solutions for complex OCR tasks but is great for demo purposes and basic OCR needs.
Certainly! Below is the complete code for a simple HTML application that performs OCR on an uploaded image using Tesseract.js, all contained within a single HTML file. You can save this code as an index.html file and open it in a web browser to see it in action.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OCR Image Upload</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f7f8fa;
}
.container {
text-align: center;
max-width: 500px;
padding: 20px;
background: #fff;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}
#imagePreview {
margin-top: 15px;
max-height: 200px;
overflow: hidden;
}
#imagePreview img {
max-width: 100%;
border-radius: 8px;
}
#ocrOutput {
margin-top: 20px;
padding: 10px;
background: #e9ecef;
border-radius: 8px;
}
</style>
</head>
<body>
<div class="container">
<h1>Image to OCR Text</h1>
<input type="file" id="imageUpload" accept="image/*" />
<div id="imagePreview"></div>
<button id="processButton">Start OCR</button>
<div id="ocrOutput"></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/tesseract.js@2/dist/tesseract.min.js"></script>
<script>
document.getElementById('imageUpload').addEventListener('change', handleImageUpload);
document.getElementById('processButton').addEventListener('click', performOCR);
let selectedImage = null;
function handleImageUpload(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
document.getElementById('imagePreview').innerHTML = `<img src="${e.target.result}" alt="Image Preview"/>`;
selectedImage = e.target.result;
};
reader.readAsDataURL(file);
}
}
function performOCR() {
if (!selectedImage) {
return alert('Please upload an image first');
}
Tesseract.recognize(
selectedImage,
'eng',
{
logger: info => console.log(info)
}
).then(({ data: { text } }) => {
document.getElementById('ocrOutput').textContent = text;
}).catch(err => {
console.error(err);
alert('An error occurred during OCR processing');
});
}
</script>
</body>
</html>
.html file, for example, index.html.This simple application should work for basic OCR needs. For more advanced OCR tasks, consider using server-side solutions or integrating more sophisticated libraries.
Not yet included in a published release.