Scan a barcode or a QR code
Press the camera button on the right of the field and hold a code in front of the camera: the value is filled in, with a short beep. USB and Bluetooth barcode scanners work too, because they type like a keyboard.
Note: Try it
Press the camera button and allow the camera. Hold any barcode up to it, for example the one on the back of a book: the number goes into the field and the window closes. Press Esc or × to close the window without reading. On a computer without a camera, the window says there is no camera.
Only some kinds of codes
->formats([BarcodeFormat::Ean13]) reads only EAN-13 (JAN) product barcodes and ignores QR codes. Typed numbers are checked too, including the check digit.
Note: Try it
Type 4006381333931 and press “Show what would be saved”: it is accepted. Change the last digit to 2 and press it again: “This doesn’t look like a valid code”.
Read the code from a photo
The scan window also has “Use a photo instead”, for computers without a camera or when the camera is blocked. On a phone it offers to take a photo; on a computer it opens the file picker.
Note: Try it
Press the camera button, then “Use a photo instead”, and choose a photo or a screenshot of a QR code. The code’s text goes into the field.
No beep and no vibration
->scan(sound: false, vibrate: false) reads codes silently, for example in an office or a library. By default a code read makes a short beep, and a short vibration on Android phones.
Note: Try it
Press the camera button and hold a barcode up to it: the value is filled in without a sound.
One code after another, one row each
In a repeater, ->scan(continuous: true) keeps the camera open. Each code read goes into the next empty row, and a new row is added when there is none, with the other fields’ defaults (quantity 1). A code is counted once while it stays in front of the camera.
Note: Try it
Press the camera button in the first row and show three different barcodes, one after the other: three rows are filled in. Move a code away and back to read it a second time. Press “Done” to close the window.
Keep scanning into one field
Outside a repeater, ->scan(continuous: true) keeps the window open, puts each new code in the field and lists the codes read so far.
Note: Try it
Press the camera button and show two barcodes: the window lists both, and the field keeps the last one.
Scanning in other fields
The camera button is the CanScanCodes trait. IdInput and WarpTextInput have it already, so ->scan() works on them. Add the trait to your own TextInput class to get it there too.
Note: Try it
This is an IdInput for ISBNs. Type 0-306-40615-2 and click outside the field: it becomes 9780306406157. Or press the camera button and show the barcode on the back of a book.
Check codes in any field
new Barcode([BarcodeFormat::Ean13]) is a validation rule for fields without a camera, for imports and for the API. This field is a plain Filament TextInput.
Note: Try it
Type 4006381333932 and press “Show what would be saved”: the check digit is wrong, so it is refused. Then type 4006381333931.
Text from a photo, on one line
->scanText() adds a “Read text from a photo” button. The text is read in the browser and shown in a window to check and correct. “Use this text” puts it in the field, on one line in a one-line field. ->scan(false) hides the camera button for codes.
Note: Try it
Press the button with the document icon and choose a photo with a serial number or any printed text. The first time, the reader takes a few seconds to get ready. Fix a letter in the window if needed, then press “Use this text”.
Text from a photo, in a text area
WarpTextarea has ->scanText() too, with the button next to the label. The lines stay as they are. For Filament’s own Textarea, add the CanScanText trait to a class of your own.
Note: Try it
Press the button next to the label and choose a photo of a delivery note or a page of text: the lines are kept.
Fill several fields from one photo
->parseUsing() gets the text that was read and returns values for other fields of the form: here the first line is the name, and the email address and phone number are found in the text. The scanned field keeps the full text.
Note: Try it
Press the button next to “Business card” and choose a photo of a business card. Press “Use this text”: the name, email and phone fields are filled in.
Languages of the text
By default the text is read in the screen’s language and English. ->scanText(languages: ['eng', 'deu']) reads English and German, so letters such as ü and ß come out right. Each language is downloaded once (2–4 MB) and kept by the browser.
Note: Try it
Choose a photo of a German text, for example “Bitte bis Freitag prüfen”: the ü is kept.
Read the text on your server
->scanText(using: fn (string $contents, string $mimeType) => ...) sends the photo to your server instead (shrunk to 2000 pixels, as a JPEG), so you can use your own OCR or AI service. ->ocrUsing() on the plugin does the same for every field. Here the server only says what it received.
Note: Try it
Press the button and choose any photo: the window shows “The server received a … photo”, with its size.
Speak instead of typing
->dictation() adds a microphone button. Press it and speak: the words go in where the cursor is. Words still being recognized show in grey under the field. The language is the screen’s language. In Firefox, which can’t do this, the button is not shown.
Note: Try it
Press the microphone button, allow the microphone and say “Call the driver at three”. Press the button again or Esc to stop.
Dictation in another language
->dictation(language: 'en-GB') listens for British English whatever the screen’s language. Chrome sends the sound to Google to turn it into text, so it needs an internet connection.
Note: Try it
Put the cursor after a word, press the microphone button and speak a sentence: it goes in at the cursor, after a space.