PHP code example of sghimire / mobile-scanner

1. Go to this page and download the library: Download sghimire/mobile-scanner library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sghimire / mobile-scanner example snippets


Scanner::scan()->gallery(false)->scan();

use Sandip\Scanner\Native\Facades\Scanner;

// Scan a single QR code with defaults
Scanner::scan()->scan();

// Fully configured
Scanner::scan()
    ->id('ticket-scanner')                    // correlate this scan with its events
    ->prompt('Scan your ticket')               // text shown above the camera viewfinder
    ->formats(['qr', 'ean13'])                 // one or more barcode formats to detect
    ->continuous()                             // keep scanning after each match
    ->scan();

use Sandip\Scanner\Native\Facades\Scanner;

Scanner::stop();                 // stop whatever scanner is open
Scanner::stop('ticket-scanner');  // stop a specific session by id

use Sandip\Scanner\Native\Events\Scanner\CodeScanned;
use Sandip\Scanner\Native\Events\Scanner\Cancelled;
use Illuminate\Support\Facades\Event;

Event::listen(function (CodeScanned $event) {
    $event->data;   // string — the decoded value
    $event->format; // string — which format matched, e.g. "qr"
    $event->id;     // ?string — matches the id you passed to ->id(), if any
});

Event::listen(function (Cancelled $event) {
    $event->reason; // ?string — e.g. "user_cancelled", "stopped_by_app", "timeout", "camera_error"
    $event->id;     // ?string
});

use Livewire\Component;
use Sandip\Scanner\Native\Attributes\OnNative;
use Sandip\Scanner\Native\Events\Scanner\CodeScanned;
use Sandip\Scanner\Native\Events\Scanner\Cancelled;
use Sandip\Scanner\Native\Facades\Scanner;

class TicketScanner extends Component
{
    public ?string $result = null;

    public function startScan(): void
    {
        Scanner::scan()->id('ticket-scanner')->prompt('Scan your ticket')->scan();
    }

    #[OnNative(CodeScanned::class)]
    public function onCodeScanned(string $data, string $format, ?string $id): void
    {
        $this->result = $data;
    }

    #[OnNative(Cancelled::class)]
    public function onCancelled(?string $reason, ?string $id): void
    {
        // handle the user closing the scanner
    }
}

// routes/api.php
Route::post('/tickets/check-in', CheckInTicketController::class);

// app/Http/Controllers/CheckInTicketController.php
namespace App\Http\Controllers;

use App\Models\Ticket;
use Illuminate\Http\Request;

class CheckInTicketController extends Controller
{
    public function __invoke(Request $request)
    {
        $ticket = Ticket::where('code', $request->string('code'))->first();

        if (! $ticket || $ticket->checked_in_at) {
            return response()->json(['valid' => false], 422);
        }

        $ticket->update(['checked_in_at' => now()]);

        return response()->json(['valid' => true, 'name' => $ticket->holder_name]);
    }
}

// app/Livewire/TicketScanner.php
namespace App\Livewire;

use App\Models\Ticket;
use Livewire\Component;
use Sandip\Scanner\Native\Attributes\OnNative;
use Sandip\Scanner\Native\Events\Scanner\CodeScanned;
use Sandip\Scanner\Native\Facades\Scanner;

class TicketScanner extends Component
{
    public array $log = [];

    public function startScanning(): void
    {
        Scanner::scan()
            ->id('check-in')
            ->prompt('Scan a ticket')
            ->formats(['qr'])
            ->continuous()
            ->scan();
    }

    public function stopScanning(): void
    {
        Scanner::stop('check-in');
    }

    #[OnNative(CodeScanned::class)]
    public function onCodeScanned(string $data): void
    {
        $ticket = Ticket::where('code', $data)->first();
        $valid = $ticket && ! $ticket->checked_in_at;

        if ($valid) {
            $ticket->update(['checked_in_at' => now()]);
        }

        array_unshift($this->log, [
            'name' => $ticket->holder_name ?? $data,
            'valid' => $valid,
        ]);
    }

    public function render()
    {
        return view('livewire.ticket-scanner');
    }
}
bash
php artisan native:plugin:register
blade
{{-- resources/views/livewire/ticket-scanner.blade.php --}}
<div>
    <button wire:click="startScanning">Start Check-In</button>
    <button wire:click="stopScanning">Stop</button>

    <ul>
        @foreach ($log as $entry)
            <li class="{{ $entry['valid'] ? 'text-green-600' : 'text-red-600' }}">
                {{ $entry['name'] }} — {{ $entry['valid'] ? 'Checked in' : 'Invalid/duplicate' }}
            </li>
        @endforeach
    </ul>
</div>