From 1b1dadda6c4f24414eff5bc9b98fa83f9eda5a6d Mon Sep 17 00:00:00 2001
From: spark
Date: Wed, 28 Oct 2026 01:10:29 +0000
Subject: [PATCH] Site: open the front camera in-page for Tirar foto
iPhone file+capture often returns HEIC or fails the hidden input.
Use getUserMedia like the kiosk so the probe is always JPEG.
---
.../site/src/app/arquivo/[id]/FindByPhoto.tsx | 33 ++--
.../src/app/galeria/[slug]/GalleryView.tsx | 31 ++--
apps/site/src/lib/SelfieCamera.tsx | 149 ++++++++++++++++++
apps/site/src/lib/asJpeg.ts | 39 ++++-
docs/ARCHITECTURE.md | 4 +-
5 files changed, 215 insertions(+), 41 deletions(-)
create mode 100644 apps/site/src/lib/SelfieCamera.tsx
diff --git a/apps/site/src/app/arquivo/[id]/FindByPhoto.tsx b/apps/site/src/app/arquivo/[id]/FindByPhoto.tsx
index 3517d41..1873e25 100644
--- a/apps/site/src/app/arquivo/[id]/FindByPhoto.tsx
+++ b/apps/site/src/app/arquivo/[id]/FindByPhoto.tsx
@@ -2,6 +2,7 @@
import { useEffect, useMemo, useState } from "react";
import { asJpeg } from "../../../lib/asJpeg";
+import { SelfieCamera } from "../../../lib/SelfieCamera";
import { day, download, Forbidden, LEVEL, Unauthorised, type FindByPhotoResult, type Level, type Me, type PhotoFace } from "./api";
import { Lightbox } from "./Lightbox";
import { Icon } from "./parts";
@@ -35,6 +36,7 @@ export function FindByPhoto({
const [result, setResult] = useState(null);
const [only, setOnly] = useState(null);
const [index, setIndex] = useState(null);
+ const [camera, setCamera] = useState(false);
// Start the face engine while the editor is on this page, so the first upload does not wait on model load.
useEffect(() => {
@@ -121,28 +123,15 @@ export function FindByPhoto({
Carrega uma fotografia com a cara bem visível (retrato, recorte de imprensa). Mostramos as fotos do arquivo em que essa pessoa aparece. A foto só serve para esta pesquisa: não fica guardada.
-
-
);
}
diff --git a/apps/site/src/lib/SelfieCamera.tsx b/apps/site/src/lib/SelfieCamera.tsx
new file mode 100644
index 0000000..4f49202
--- /dev/null
+++ b/apps/site/src/lib/SelfieCamera.tsx
@@ -0,0 +1,149 @@
+"use client";
+
+import { useEffect, useRef, useState, type CSSProperties } from "react";
+
+/**
+ * Front-camera capture in the page (getUserMedia → canvas JPEG). Prefer this over
+ * `` on iPhone: the system picker often returns HEIC the face API cannot read,
+ * and a hidden file input inside a label is unreliable on iOS Safari.
+ */
+export function SelfieCamera({ onShot, onCancel }: { onShot: (file: File) => void; onCancel: () => void }) {
+ const video = useRef(null);
+ const [problem, setProblem] = useState(null);
+ const [busy, setBusy] = useState(false);
+
+ useEffect(() => {
+ let stream: MediaStream | null = null;
+ let cancelled = false;
+ const start = async () => {
+ if (!navigator.mediaDevices?.getUserMedia) {
+ setProblem("Este browser não dá acesso à câmara. Usa «Escolher da galeria» ou abre no Safari/Chrome.");
+ return;
+ }
+ try {
+ stream = await navigator.mediaDevices.getUserMedia({
+ video: { facingMode: { ideal: "user" }, width: { ideal: 1280 }, height: { ideal: 960 } },
+ audio: false,
+ });
+ if (cancelled) {
+ stream.getTracks().forEach((t) => t.stop());
+ return;
+ }
+ const el = video.current;
+ if (el) {
+ el.srcObject = stream;
+ await el.play().catch(() => undefined);
+ }
+ } catch {
+ if (!cancelled) setProblem("Não foi possível abrir a câmara. Autoriza a câmara neste site e tenta outra vez.");
+ }
+ };
+ void start();
+ return () => {
+ cancelled = true;
+ stream?.getTracks().forEach((t) => t.stop());
+ };
+ }, []);
+
+ const shoot = () => {
+ const v = video.current;
+ if (!v || !v.videoWidth || busy) return;
+ setBusy(true);
+ const canvas = document.createElement("canvas");
+ canvas.width = v.videoWidth;
+ canvas.height = v.videoHeight;
+ const ctx = canvas.getContext("2d");
+ if (!ctx) {
+ setProblem("Não foi possível capturar a imagem.");
+ setBusy(false);
+ return;
+ }
+ ctx.drawImage(v, 0, 0);
+ canvas.toBlob(
+ (blob) => {
+ setBusy(false);
+ if (!blob) {
+ setProblem("Não foi possível capturar a imagem.");
+ return;
+ }
+ onShot(new File([blob], "foto.jpg", { type: "image/jpeg" }));
+ },
+ "image/jpeg",
+ 0.92,
+ );
+ };
+
+ return (
+
+ {problem ? (
+ <>
+
+ {problem}
+
+
+ >
+ ) : (
+ <>
+
+
+
+
+
+ >
+ )}
+
+ );
+}
+
+const btnPrimary: CSSProperties = {
+ padding: "12px 22px",
+ fontSize: 16,
+ fontWeight: 600,
+ border: 0,
+ borderRadius: 8,
+ background: "#fff",
+ color: "#111",
+ cursor: "pointer",
+};
+
+const btnGhost: CSSProperties = {
+ ...btnPrimary,
+ background: "transparent",
+ color: "#fff",
+ border: "1px solid rgba(255,255,255,0.45)",
+};
diff --git a/apps/site/src/lib/asJpeg.ts b/apps/site/src/lib/asJpeg.ts
index 1b0a957..b15ea5b 100644
--- a/apps/site/src/lib/asJpeg.ts
+++ b/apps/site/src/lib/asJpeg.ts
@@ -7,12 +7,8 @@ const JPEG_MAX_SIDE = 1600;
export async function asJpeg(file: File): Promise {
const already = /^image\/(jpeg|jpg|png|webp)$/i.test(file.type);
if (already && file.size <= 3 * 1024 * 1024) return file;
- let bitmap: ImageBitmap;
- try {
- bitmap = await createImageBitmap(file);
- } catch {
- throw new Error("convert_failed");
- }
+
+ const bitmap = await decode(file);
try {
const scale = Math.min(1, JPEG_MAX_SIDE / Math.max(bitmap.width, bitmap.height));
const w = Math.max(1, Math.round(bitmap.width * scale));
@@ -22,7 +18,7 @@ export async function asJpeg(file: File): Promise {
canvas.height = h;
const ctx = canvas.getContext("2d");
if (!ctx) throw new Error("convert_failed");
- ctx.drawImage(bitmap, 0, 0, w, h);
+ ctx.drawImage(bitmap.source, 0, 0, w, h);
const blob = await new Promise((resolve) => canvas.toBlob(resolve, "image/jpeg", 0.92));
if (!blob) throw new Error("convert_failed");
return new File([blob], "foto.jpg", { type: "image/jpeg" });
@@ -30,3 +26,32 @@ export async function asJpeg(file: File): Promise {
bitmap.close();
}
}
+
+type Decoded = { source: CanvasImageSource; width: number; height: number; close: () => void };
+
+async function decode(file: File): Promise {
+ try {
+ const bitmap = await createImageBitmap(file);
+ return { source: bitmap, width: bitmap.width, height: bitmap.height, close: () => bitmap.close() };
+ } catch {
+ // Safari sometimes cannot createImageBitmap(HEIC) even when can paint it.
+ }
+ const url = URL.createObjectURL(file);
+ try {
+ const img = await new Promise((resolve, reject) => {
+ const el = new Image();
+ el.onload = () => resolve(el);
+ el.onerror = () => reject(new Error("convert_failed"));
+ el.src = url;
+ });
+ return {
+ source: img,
+ width: img.naturalWidth || img.width,
+ height: img.naturalHeight || img.height,
+ close: () => URL.revokeObjectURL(url),
+ };
+ } catch (err) {
+ URL.revokeObjectURL(url);
+ throw err instanceof Error ? err : new Error("convert_failed");
+ }
+}
diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md
index 76709f2..d1ecd31 100644
--- a/docs/ARCHITECTURE.md
+++ b/docs/ARCHITECTURE.md
@@ -526,7 +526,9 @@ run on the `next` containers (the phase gate).
/v1/orgs/:orgId/faces/lookup`; on the sites, "Encontra-te" finds a visitor's face in one gallery, `POST
/v1/public/galleries/:slug/find`; in the archive, editors on the cloud with the option on search the whole
archive they can see, `POST /v1/archive/:source/find`). Only with biometrics on; the search photo is read in
- memory and never kept. The API runs its own face engine for this, started on the first search and stopped
+ memory and never kept. On phones, "Tirar foto" uses the front camera in the page (`getUserMedia` → JPEG),
+ not `` (iOS often hands HEIC the API cannot read). Gallery picks still convert to JPEG in
+ the browser before upload. The API runs its own face engine for this, started on the first search and stopped
after 10 idle minutes.
6. Cutover: final import, redirects, old projects read-only, then switched off.
--
2.47.3