feat(android): Capacitor APK build + native save/share bridge

- android/ — Capacitor project (build/, local.properties ignored)
- app/js/native.js — bridge สำหรับ APK: savePhoto/saveZip/shareItems ผ่าน Filesystem+Share
- app/js/csv.js — นำเข้าโฟลเดอร์หลายรายการจาก CSV (site,letter,datetime,name)
- app/js/orient.js — จัดการ orientation ของ preview/กล้อง
- app/manual.html + APK.md — คู่มือใช้งานและวิธี build APK
- app/templates/folders-template.csv — เทมเพลต CSV
- package.json / package-lock.json / capacitor.config.json / scripts/build-apk.sh
- ปรับ ui, camera, exif, archive, db, naming, main, sw + css/README ตามงานข้างบน
This commit is contained in:
Bkk1234 committed 2026-11-20 08:50:42 +07:00
1 parent a0bccc2316
commit 6f146aef59
75 files changed
+4637 -151

No files matched your search

+382 -46
View File
@@ -11,10 +11,14 @@ import { detectDevice, exifModelName, environment } from "./device.js";
import {
defaultFolderName, exifDateString, safeFileName, uniqueName,
} from "./naming.js";
import { ensureZip, invalidateZip, downloadFile, shareFile, canShareFiles } from "./archive.js";
import { ensureZip, peekZip, invalidateZip, downloadFile, shareFile, canShareFiles, buildAllZip } from "./archive.js";
import { parseCsv, planFolders } from "./csv.js";
import * as native from "./native.js";
import { autoOrient, detectOrientation, cropToRatio, ratioValue, makeThumbFromBlob, rotateBlob } from "./orient.js";
const LS_FOLDER = "camcat.activeFolder";
const LS_FACING = "camcat.facing";
const LS_RATIO = "camcat.ratio";
const state = {
view: "camera",
@@ -30,7 +34,9 @@ const state = {
zoomRange: null,
zoomValue: null,
grid: false,
ratio: localStorage.getItem(LS_RATIO) || "4:3",
busy: false,
lastZip: null,
facing: localStorage.getItem(LS_FACING) || "environment",
storage: null,
};
@@ -105,6 +111,11 @@ function flash() {
setTimeout(() => el.remove(), 220);
}
function newUniqueId(ts = Date.now()) {
const rnd = () => Math.floor(Math.random() * 0xffff).toString(16).padStart(4, "0");
return (ts.toString(16) + rnd().repeat(8)).padEnd(32, "0").slice(0, 32);
}
function nextFileName(folderName, images) {
const base = safeFileName(folderName).replace(/\s+/g, "_");
let max = 0;
@@ -142,6 +153,20 @@ async function getGps() {
}
// ── camera view ─────────────────────────────────────────────
function fitFrame() {
const stage = document.querySelector(".stage");
const frame = document.getElementById("cam-frame");
if (!stage || !frame) return;
const box = stage.getBoundingClientRect();
if (!box.width || !box.height) return;
const r = state.ratio === "3:4" ? 3 / 4 : 4 / 3;
let w = box.width;
let h = w / r;
if (h > box.height) { h = box.height; w = h * r; }
frame.style.width = Math.round(w) + "px";
frame.style.height = Math.round(h) + "px";
}
function syncCam(overrides = {}) {
ui.syncCameraControls({
torchAvailable: state.torchAvailable && state.camReady,
@@ -150,10 +175,13 @@ function syncCam(overrides = {}) {
zoomValue: state.zoomValue,
gridOn: state.grid,
count: state.images.length,
ratio: state.ratio,
thumbUrl: state.images.length ? thumbUrl(state.images[state.images.length - 1]) : null,
ready: state.camReady,
...overrides,
});
fitFrame();
setTimeout(fitFrame, 80);
}
function renderCameraView() {
@@ -274,6 +302,12 @@ async function switchCamera() {
}
// ── shoot ───────────────────────────────────────────────────
const withTimeout = (p, ms, label) =>
Promise.race([
p,
new Promise((_, rej) => setTimeout(() => rej(new Error(label + " timeout")), ms)),
]);
async function shoot() {
if (state.busy) return;
if (!state.camReady) { ui.toast("กล้องยังไม่พร้อม"); return; }
@@ -281,25 +315,61 @@ async function shoot() {
const shutter = document.getElementById("shutter");
if (shutter) shutter.disabled = true;
// watchdog: บางเครื่อง promise ของกล้องค้าง → ปลดล็อกปุ่มเอง
const watchdog = setTimeout(() => {
state.busy = false;
const s = document.getElementById("shutter");
if (s) s.disabled = false;
ui.toast("ถ่ายใช้เวลานานเกินไป — ลองอีกครั้ง");
}, 20000);
try {
const folder = state.folder;
if (!folder) throw new Error("ยังไม่มีโฟลเดอร์");
const shot = await camApi.captureStill();
const gps = await getGps();
const at = Date.now();
const exifBlob = await writeExif(shot.blob, {
make: "Samsung",
model: exifModelName(),
dateTime: exifDateString(new Date(at)),
gps,
const shot = await Promise.race([
camApi.captureStill(),
new Promise((_, rej) => setTimeout(() => rej(new Error("capture timeout")), 12000)),
]);
// ① อ่าน EXIF Orientation ของไฟล์ แล้วหมุนพิกเซลให้ตั้งตรง (Orientation = 1)
const orientationTag = await detectOrientation(shot.blob);
const oriented = await autoOrient({
blob: shot.blob,
exifOrientation: orientationTag,
width: shot.width,
height: shot.height,
folder: folder.name,
source: shot.source,
version: APP_VERSION,
facing: shot.facing,
});
const exifRead = await readExif(exifBlob);
// ② ครอปตามอัตราส่วนที่เลือก (ทำหลังจัดทิศทาง เพื่อไม่ให้ครอปผิดแกน)
const cropped = await cropToRatio(oriented.blob, ratioValue(state.ratio));
const finalBlob = cropped.blob;
const fw = cropped.width || oriented.width || shot.width;
const fh = cropped.height || oriented.height || shot.height;
const thumb = await makeThumbFromBlob(finalBlob);
const gps = await getGps();
const at = Date.now();
const isSamsung = /^SM-/i.test(device.code) || device.brand === "Samsung";
const exifBlob = await writeExif(finalBlob, {
make: isSamsung ? "samsung" : device.brand || "CamCat",
model: /^SM-/i.test(device.code)
? device.code
: /^[\x20-\x7E]+$/.test(device.model || "")
? device.model
: "CamCat Web Camera",
modelName: exifModelName(),
software: `CamCat ${APP_VERSION} | Android ${device.androidVersion || "-"} | Chrome ${device.chromeVersion || "-"}`,
dateTime: exifDateString(new Date(at)),
gps,
width: fw,
height: fh,
folder: folder.name,
orientation: 1,
uniqueId: newUniqueId(at),
});
const exifRead = await withTimeout(readExif(exifBlob), 6000, "readExif");
const name = nextFileName(folder.name, state.images);
const record = {
@@ -307,17 +377,22 @@ async function shoot() {
folderId: folder.id,
name,
blob: exifBlob,
thumb: shot.thumb,
thumb: thumb,
size: exifBlob.size,
createdAt: at,
meta: {
width: shot.width,
height: shot.height,
width: fw,
height: fh,
ratio: state.ratio,
orientationTag,
rotationApplied: oriented.rotation || 0,
mirrored: !!oriented.mirrored,
device: exifModelName(),
facing: shot.facing,
facingLabel: shot.facing === "environment" ? "กล้องหลัง" : "กล้องหน้า",
source: shot.source,
sourceLabel: shot.source === "imagecapture" ? "ImageCapture (ความละเอียดเต็ม)" : "วิดีโอ (canvas)",
orientSource: oriented.source,
gps: gps ? { lat: gps.lat, lng: gps.lng, accuracy: gps.accuracy } : null,
exif: exifRead,
app: `CamCat ${APP_VERSION}`,
@@ -325,24 +400,76 @@ async function shoot() {
},
};
await db.putImage(record);
await db.touchFolder(folder.id);
await withTimeout(db.putImage(record), 8000, "db.putImage");
await withTimeout(db.touchFolder(folder.id), 5000, "db.touchFolder");
state.images.push(record);
state.counts = state.counts.map((c, i) => (state.folders[i]?.id === folder.id ? c + 1 : c));
flash();
invalidateZip();
syncCam({ thumbUrl: thumbUrl(record) });
ui.toast(`บันทึก ${name} · ${shot.width}×${shot.height}`);
let savedTo = "";
if (native.isNative()) {
const uri = await native.savePhoto(folder.name, name, exifBlob);
if (uri) savedTo = ` → ${native.displayPath(folder.name)}`;
}
ui.toast(`บันทึก ${name}${savedTo}`);
} catch (err) {
console.error(err);
ui.toast("ถ่ายไม่สำเร็จ: " + (err.code || err.message));
} finally {
clearTimeout(watchdog);
state.busy = false;
if (shutter) shutter.disabled = false;
}
}
async function rotatePhoto(id, deg) {
const rec = await db.getImage(id);
if (!rec) return;
try {
const out = await rotateBlob(rec.blob, deg);
const at = rec.createdAt;
const folderName = rec.meta?.folder || state.folder?.name || "";
const exifBlob = await writeExif(out.blob, {
make: rec.meta?.exif?.make || "samsung",
model: rec.meta?.exif?.model || "SM-S911B",
software: rec.meta?.exif?.software || `CamCat ${APP_VERSION}`,
dateTime: exifDateString(new Date(at)),
width: out.width,
height: out.height,
folder: folderName,
orientation: 1,
uniqueId: rec.meta?.exif?.uniqueId || newUniqueId(at),
gps: rec.meta?.gps
? { lat: rec.meta.gps.lat, lng: rec.meta.gps.lng, accuracy: rec.meta.gps.accuracy }
: null,
});
rec.blob = exifBlob;
rec.thumb = out.thumb;
rec.size = exifBlob.size;
rec.meta = {
...(rec.meta || {}),
width: out.width,
height: out.height,
rotationApplied: (((rec.meta?.rotationApplied || 0) + deg) % 360 + 360) % 360,
rotatedManually: true,
exif: await readExif(exifBlob),
};
await db.putImage(rec);
state.images = state.images.map((i) => (i.id === id ? rec : i));
dropThumb(id);
invalidateZip();
await openPhoto(id);
ui.toast(`หมุน ${deg > 0 ? "ขวา" : "ซ้าย"} 90° แล้ว`);
} catch (err) {
console.error(err);
ui.toast("หมุนรูปไม่สำเร็จ: " + err.message);
}
}
// ── folder data ─────────────────────────────────────────────
async function loadFolders() {
state.folders = await db.getFolders();
@@ -363,7 +490,7 @@ async function selectFolder(id, { silent = false } = {}) {
async function ensureDefaultFolder() {
await loadFolders();
if (!state.folders.length) {
const folder = { id: "f_" + Date.now(), name: defaultFolderName(), createdAt: Date.now(), updatedAt: Date.now() };
const folder = { id: "f_" + Date.now(), name: defaultFolderName(state.folders.map((f) => f.name)), createdAt: Date.now(), updatedAt: Date.now() };
await db.putFolder(folder);
await loadFolders();
await selectFolder(folder.id, { silent: true });
@@ -438,43 +565,105 @@ function schedulePrebuild() {
}, 1200);
}
async function buildZipFile() {
const { file } = await ensureZip(state.folder, state.images, (p) =>
ui.progressUpdate(p, `กำลังรวม ${state.images.length} รูป`)
);
state.lastZip = file;
return file;
}
function sharePayload(file) {
return {
title: state.folder?.name || file.name,
text: `รูปจาก ${state.folder?.name || ""} (${state.images.length} รูป)`,
};
}
function shareReasonText(reason) {
if (reason === "notallowed") return "เบราว์เซอร์ตัดสิทธิ์เพราะรอนาน — แตะอีกครั้ง";
if (reason === "nofiles") return "เบราว์เซอร์นี้ไม่รองรับการแชร์ไฟล์";
if (reason === "notsupported" || reason === "unsupported") return "ไม่มี Web Share API";
if (reason === "failed") return "ระบบแชร์ปฏิเสธ";
return "";
}
function showShareSheet(file, reason = "") {
ui.openSheet(
ui.shareReadySheet({
name: file.name,
size: file.size,
canShare: canShareFiles(file) && !!navigator.share,
reason: shareReasonText(reason),
})
);
}
async function handleShareResult(res, file) {
if (res.ok) { ui.closeSheet(); ui.toast("เปิด share sheet ของเครื่องแล้ว"); return; }
if (res.reason === "cancelled") { ui.closeSheet(); return; }
showShareSheet(file, res.reason);
}
async function deliverFile(file, note = "") {
if (native.isNative()) {
const uri = await native.saveZip(file);
if (uri) {
ui.toast(`บันทึก ${file.name} ลง Documents/CamCat-ZIP${note ? " · " + note : ""}`);
return;
}
}
downloadFile(file);
ui.toast(`ดาวน์โหลด ${file.name} (${ui.fmtBytes(file.size)})${note ? " · " + note : ""}`);
}
async function zipFlow(mode) {
if (!state.folder) { ui.toast("ยังไม่เลือกโฟลเดอร์"); return; }
if (!state.images.length) { ui.toast("ยังไม่มีรูปในโฟลเดอร์นี้"); return; }
if (state.busy) return;
state.busy = true;
ui.progressSheet(`กำลังรวม ${state.images.length} รูปเป็น ZIP`);
try {
const { file } = await ensureZip(state.folder, state.images, (p) =>
ui.progressUpdate(p, `กำลังรวม ${state.images.length} รูป`)
);
ui.closeSheet();
if (mode === "download") {
downloadFile(file);
ui.toast(`ดาวน์โหลด ${file.name} (${ui.fmtBytes(file.size)})`);
const cached = peekZip(state.folder, state.images);
let file = cached;
if (!file) {
ui.progressSheet(`กำลังรวม ${state.images.length} รูปเป็น ZIP`);
file = await buildZipFile();
ui.closeSheet();
}
await deliverFile(file);
return;
}
if (!canShareFiles(file)) {
downloadFile(file);
ui.toast("เครื่องนี้แชร์ไฟล์ผ่านเว็บไม่ได้ — ดาวน์โหลด ZIP ให้แล้ว");
// APK: ใช้ share sheet ของ Android ผ่านปลั๊กอิน Share (FileProvider)
if (native.isNative()) {
let file = peekZip(state.folder, state.images);
if (!file) {
ui.progressSheet(`กำลังรวม ${state.images.length} รูปเป็น ZIP`);
file = await buildZipFile();
ui.closeSheet();
}
const res = await native.shareItems([{ name: file.name, blob: file }], state.folder?.name || "");
if (res.ok) { ui.closeSheet(); ui.toast("แชร์ผ่านระบบของ Android แล้ว"); }
else if (res.reason === "cancelled") { ui.closeSheet(); }
else { await deliverFile(file); }
return;
}
const res = await shareFile(file, {
title: state.folder.name,
text: `รูปจาก ${state.folder.name} (${state.images.length} รูป)`,
});
if (res.ok) ui.toast("เปิดเมนูแชร์ของเครื่องแล้ว");
else if (res.reason === "cancelled") ui.toast("ยกเลิกการแชร์");
else if (res.reason === "unsupported" || res.reason === "nofiles") {
downloadFile(file);
ui.toast("แชร์ไฟล์ไม่ได้ — ดาวน์โหลด ZIP ให้แล้ว");
} else {
downloadFile(file);
ui.toast("แชร์ไม่สำเร็จ — ดาวน์โหลด ZIP ให้แล้ว");
// แชร์: ถ้ามี ZIP ที่สร้างไว้แล้ว → ยิง share ทันทีในจังหวะแตะ (activation ยังสด)
const cached = peekZip(state.folder, state.images);
if (cached) {
const res = await shareFile(cached, sharePayload(cached));
await handleShareResult(res, cached);
return;
}
// ยังไม่มี ZIP → สร้างก่อน แล้วให้ผู้ใช้แตะแชร์อีกครั้ง (activation ใหม่)
ui.progressSheet(`กำลังรวม ${state.images.length} รูปเป็น ZIP`);
const file = await buildZipFile();
ui.closeSheet();
showShareSheet(file, "");
} catch (err) {
console.error(err);
ui.closeSheet();
@@ -484,6 +673,53 @@ async function zipFlow(mode) {
}
}
// ZIP ทุกโฟลเดอร์ในครั้งเดียว (แยกโฟลเดอร์ย่อยในไฟล์ zip)
async function zipAllFolders() {
if (state.busy) return;
await loadFolders();
if (!state.folders.length) { ui.toast("ยังไม่มีโฟลเดอร์"); return; }
const byFolder = {};
let total = 0;
for (const f of state.folders) {
const imgs = await db.getImages(f.id);
byFolder[f.id] = imgs;
total += imgs.length;
}
if (!total) { ui.toast("ยังไม่มีรูปในทุกโฟลเดอร์"); return; }
state.busy = true;
ui.progressSheet(`กำลังรวม ${state.folders.length} โฟลเดอร์ (${total} รูป)`);
try {
const { file, photos, folders } = await buildAllZip(state.folders, byFolder, (p) =>
ui.progressUpdate(p, `กำลังรวม ${total} รูป`)
);
ui.closeSheet();
await deliverFile(file, `${folders} โฟลเดอร์ ${photos} รูป`);
} catch (err) {
console.error(err);
ui.closeSheet();
ui.toast("สร้าง ZIP รวมไม่สำเร็จ: " + err.message);
} finally {
state.busy = false;
}
}
async function shareCachedZip() {
const file = peekZip(state.folder, state.images) || state.lastZip;
if (!file) { ui.toast("ยังไม่มีไฟล์ ZIP"); return; }
const res = await shareFile(file, sharePayload(file));
await handleShareResult(res, file);
}
function saveCachedZip() {
const file = peekZip(state.folder, state.images) || state.lastZip;
if (!file) { ui.toast("ยังไม่มีไฟล์ ZIP"); return; }
downloadFile(file);
ui.closeSheet();
ui.toast(`บันทึก ${file.name} ลงเครื่องแล้ว`);
}
// ── photo sheet ─────────────────────────────────────────────
async function openPhoto(id) {
const rec = await db.getImage(id);
@@ -509,6 +745,12 @@ async function deletePhoto(id) {
async function shareOnePhoto(id) {
const rec = await db.getImage(id);
if (!rec) return;
if (native.isNative()) {
const res = await native.shareItems([{ name: rec.name || "photo.jpg", blob: rec.blob }], rec.name || "");
ui.toast(res.ok ? "แชร์ผ่านระบบของ Android แล้ว" : res.reason === "cancelled" ? "ยกเลิก" : "แชร์ไม่สำเร็จ");
return;
}
const file = new File([rec.blob], rec.name || "photo.jpg", { type: "image/jpeg" });
if (!canShareFiles(file)) {
downloadFile(file);
@@ -533,6 +775,9 @@ async function showDiagnostics() {
["visualViewport", env.visualViewport],
["จอจริง (px)", env.screen],
["โหมดติดตั้ง", env.standalone ? "PWA (standalone)" : "เบราว์เซอร์"],
["โหมดแอป", native.nativeInfo().label],
["ปุ่มเสียง", native.isNative() ? "กดถ่ายรูปได้ (Volume up/down)" : "ใช้ได้เฉพาะใน APK"],
["ที่เก็บไฟล์", native.isNative() ? "Documents/CamCat (โฟลเดอร์จริงในเครื่อง)" : "Download ของเบราว์เซอร์"],
["ความปลอดภัย", env.secureContext ? "HTTPS — ใช้กล้องได้" : "ไม่ปลอดภัย — กล้องถูกบล็อก"],
["URL", env.url],
["สตรีมกล้อง", s.width ? `${s.width}×${s.height} @${Math.round(s.frameRate || 0)}fps` : "ยังไม่เปิด"],
@@ -576,11 +821,67 @@ async function copyLink() {
}
}
// ── สร้างโฟลเดอร์เร็ว / นำเข้า CSV ───────────────────────────
async function quickCreateFolder() {
await loadFolders();
const name = defaultFolderName(state.folders.map((f) => f.name));
const folder = { id: "f_" + Date.now(), name, createdAt: Date.now(), updatedAt: Date.now() };
await db.putFolder(folder);
await selectFolder(folder.id, { silent: true });
await loadFolders();
await render();
ui.toast("สร้าง " + name + " — เปลี่ยนชื่อได้ที่ปุ่มดินสอ");
}
function pickCsvFile() {
const input = document.createElement("input");
input.type = "file";
input.accept = ".csv,text/csv,text/plain";
input.style.display = "none";
input.addEventListener("change", async () => {
const file = input.files && input.files[0];
input.remove();
if (!file) return;
try {
const text = await file.text();
const { rows } = parseCsv(text);
if (!rows.length) { ui.toast("ไฟล์ CSV ไม่มีแถวข้อมูล"); return; }
await loadFolders();
const { items, errors } = planFolders(rows, state.folders.map((f) => f.name));
const now = Date.now();
for (let i = 0; i < items.length; i++) {
await db.putFolder({ id: `f_${now}_${i}`, name: items[i].name, createdAt: now + i, updatedAt: now + i });
}
await loadFolders();
if (items.length) {
const first = state.folders.find((f) => f.name === items[0].name);
if (first) await selectFolder(first.id, { silent: true });
}
await render();
ui.openSheet(
ui.importResultSheet({
created: items.length,
skipped: errors.length,
errors,
total: rows.length,
})
);
if (items.length) ui.toast(`สร้าง ${items.length} โฟลเดอร์จาก CSV`);
} catch (err) {
console.error(err);
ui.toast("อ่านไฟล์ CSV ไม่สำเร็จ: " + err.message);
}
});
document.body.appendChild(input);
input.click();
}
// ── folder modals ───────────────────────────────────────────
async function openNewFolder() {
await loadFolders();
const base = defaultFolderName();
const suggested = uniqueName(base, state.folders.map((f) => f.name));
const suggested = defaultFolderName(state.folders.map((f) => f.name));
ui.openSheet(ui.folderSheet("new", { suggested }));
setTimeout(() => document.getElementById("folder-name-input")?.select(), 60);
}
@@ -636,7 +937,7 @@ async function confirmDeleteFolder(id) {
if (state.folder?.id === id) {
if (state.folders.length) await selectFolder(state.folders[0].id, { silent: true });
else {
const folder = { id: "f_" + Date.now(), name: defaultFolderName(), createdAt: Date.now(), updatedAt: Date.now() };
const folder = { id: "f_" + Date.now(), name: defaultFolderName(state.folders.map((f) => f.name)), createdAt: Date.now(), updatedAt: Date.now() };
await db.putFolder(folder);
await loadFolders();
await selectFolder(folder.id, { silent: true });
@@ -665,6 +966,13 @@ document.addEventListener("click", async (e) => {
syncCam();
break;
}
case "toggle-ratio": {
state.ratio = state.ratio === "4:3" ? "3:4" : "4:3";
localStorage.setItem(LS_RATIO, state.ratio);
syncCam();
ui.toast("อัตราส่วนภาพ " + state.ratio + " (บันทึกแล้ว)");
break;
}
case "toggle-grid":
state.grid = !state.grid;
syncCam();
@@ -677,6 +985,12 @@ document.addEventListener("click", async (e) => {
await render();
break;
case "new-folder": await openNewFolder(); break;
case "quick-new-folder": await quickCreateFolder(); break;
case "csv-sheet": ui.openSheet(ui.csvSheet()); break;
case "pick-csv": pickCsvFile(); break;
case "zip-all": await zipAllFolders(); break;
case "share-zip-now": await shareCachedZip(); break;
case "save-zip": saveCachedZip(); break;
case "rename-folder": openRenameFolder(id); break;
case "delete-folder": {
const f = state.folders.find((x) => x.id === id);
@@ -690,6 +1004,7 @@ document.addEventListener("click", async (e) => {
case "open-photo": await openPhoto(id); break;
case "delete-photo": await deletePhoto(id); break;
case "share-photo": await shareOnePhoto(id); break;
case "rotate-photo": await rotatePhoto(id, Number(el.dataset.dir || 90)); break;
case "zip-download": await zipFlow("download"); break;
case "zip-share": await zipFlow("share"); break;
case "diagnostics": await showDiagnostics(); break;
@@ -746,6 +1061,11 @@ async function init() {
document.getElementById("app").dataset.view = state.view;
renderCameraView();
// ล็อกแนวตั้ง (APK) — กัน UI หมุนตามตอนถือแนวนอนแล้วถ่ายไม่ได้
if (native.isNative() && screen.orientation && screen.orientation.lock) {
try { await screen.orientation.lock("portrait"); } catch { /* เว็บจะThrow — ข้าม */ }
}
const wanted = new URLSearchParams(location.search).get("view");
if (wanted === "gallery" || wanted === "folders") {
await navigate(wanted);
@@ -760,4 +1080,20 @@ async function init() {
}
}
// ปุ่มเพิ่ม/ลดเสียง (ส่งมาจาก MainActivity ใน APK) = ชัตเตอร์
let lastVolumeShot = 0;
window.addEventListener("camcat:shutter", () => {
const now = Date.now();
if (now - lastVolumeShot < 600) return; // กดค้าง/กดรัว
lastVolumeShot = now;
if (state.view !== "camera" || !state.camReady || state.busy) return;
if (navigator.vibrate) { try { navigator.vibrate(12); } catch { /* ignore */ } }
shoot();
});
window.addEventListener("resize", () => fitFrame());
window.addEventListener("orientationchange", () => setTimeout(fitFrame, 250));
window.addEventListener("DOMContentLoaded", init);
// เปิดให้ตรวจสถานะภายในได้ (ใช้ตอน debug บนเครื่องจริง)
window.__camcat = { state, cam, native, version: APP_VERSION };