Barcode Scanner for Capacitor Ionic Sample Code

Struggling to find a barcode scanner code example for Ionic? Discover how to integrate barcode scanning into your app using Capacitor Scanner plugin.

When searching for a barcode scanner online for Ionic, it can sometimes be challenging to find a code example that works effectively. However, after some persistent searching, you were able to discover a suitable solution.

Ionic is a popular framework for building hybrid mobile applications using web technologies such as HTML, CSS, and JavaScript. Integrating a barcode scanner into an Ionic app can be beneficial for various use cases, including inventory management, product scanning, ticketing systems, and more.

To implement a barcode scanner in Ionic, you typically need to use a plugin that provides access to the device’s camera and barcode scanning capabilities. The most commonly used plugin for barcode scanning in Ionic is the Capacitor Bar Code Scanner, which leverages the capabilities of the underlying platform (Android or iOS).

To get started, you first need to set up your Ionic project and ensure that Capacitor is properly installed. Once your project is ready, you can install the Capacitor Bar Code Scanner plugin by running the following command:

npm install @capacitor-community/barcode-scanner
npx cap sync

For iOS

+  <key>NSCameraUsageDescription</key>
+  <string>To be able to scan barcodes</string>

For Android

<?xml version="1.0" encoding="utf-8"?>
+  xmlns:tools=""

+    android:hardwareAccelerated="true"

+  <uses-permission android:name="android.permission.CAMERA" />

+  <uses-sdk tools:overrideLibrary="" />

Example Code

import { IonContent, IonHeader, IonPage, IonTitle, IonToolbar, IonButton } from '@ionic/react';
import ExploreContainer from '../components/ExploreContainer';
import './Home.css';
import { BarcodeScanner } from '@capacitor-community/barcode-scanner';

const prepare = () => {

const startScan = async () => {
  const result = await BarcodeScanner.startScan();
  if (result.hasContent) {
    const apiUrl = `#`;
    try {
      const response = await fetch(apiUrl);
      const data = await response.json();
      return data.message;
    } catch (error) {

const stopScan = () => {

const askUser = () => {

  const c = confirm('Do you want to scan a barcode?');

  if (c) {
  } else {

const Home: React.FC = () => {
  return (
          <IonTitle>Test Blank</IonTitle>
      <IonContent fullscreen>
        <IonHeader collapse="condense">
            <IonTitle size="large">TestBlank</IonTitle>
        <IonButton onClick={askUser}>Scan</IonButton>
        <ExploreContainer />

export default Home;

Full Documentation: