How to read barcode on Windows Mobile and Windows Phone 8.

This page will demonstrate how to install the SDK and use the SD-TOOLKITŪ Barcode Reader SDK API from different programming languages to successfully read 1D and 2D Barcode symbols from Image or camera device of your Windows Mobile or Windows Phone device.

Installing the SDK

  1. Download the setup, unzip it and run SDTBarcodeSetupCE.exe
  2. Follow the Setup Wizard instructions. When asked enter your Developer licnese key or leave the field blank to activate trial license on your computer. The one-time internet connection is required during installation.
  3. Once setup has finished the folder C:\Program Files\SD-Toolkit\SDTBarcodeSDKWinCE will contain runtime components, documentation and sample applications with sourcecode for your reference.

Reading Barcodes from C# Windows Phone 8 Application using Camera

The described below sample application sourcecodes are located in C:\Program Files\SD-Toolkit\SDTBarcodeSDKWinCE\Samples\WindowsPhone\SDTBrcScannerSampleWP.

show screenshots

  1. Install the SDK.
  2. In MS Visual Studio 2012 Create new Windows Phone Application:
    New Project->Visual C#->Smart Device->Windows Mobile 6 Professional->Device Application.
  3. Choose Windows Phone 8 as a target platform
  4. Modify MainPage.xaml Grid to contain the following controls definitions:
    <Grid x:Name="LayoutRoot" Background="Transparent">
    	<Grid.RowDefinitions>
    		<RowDefinition Height="Auto"/>
    		<RowDefinition Height="*"/>
    	</Grid.RowDefinitions>
    
    	<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
    		<TextBlock Text="SD-TOOLKIT BARCODE SDK SAMPLE" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
    	</StackPanel>
    
    	<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    		<RadioButton x:Name="rb1DBarcodes" Content="1D Barcodes" HorizontalAlignment="Left" VerticalAlignment="Top" IsChecked="True"/>
    		<RadioButton x:Name="rbQrcode" Content="QR-Code" HorizontalAlignment="Left" Margin="0,72,0,0" VerticalAlignment="Top"/>
    		<RadioButton x:Name="rbDatamatrix" Content="Datamatrix" HorizontalAlignment="Left" Margin="0,149,0,0" VerticalAlignment="Top"/>
    		<Button Content="SCAN" HorizontalAlignment="Left" Margin="0,238,0,0" VerticalAlignment="Top" Width="446" Height="101" Click="Button_Click"/>
    		<TextBlock HorizontalAlignment="Left" Margin="0,344,0,0" TextWrapping="Wrap" Text="Results:" VerticalAlignment="Top"/>
    		<TextBox x:Name="tbResults" Height="315" Margin="0,359,10,0" TextWrapping="Wrap" IsReadOnly="True" AcceptsReturn="True" VerticalAlignment="Top"/>
    	</Grid>
    </Grid>
    						
  5. Add project reference to SD-TOOLKIT Barcode Scanner runtime component located in C:\Program Files (x86)\SD-Toolkit\SDTBarcodeSDKWinCE\Redistributables\WindowsPhone8\ARM folder
  6. Double click on Scan button in XAML editor and add the following code to Button click event handler
           
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (rb1DBarcodes.IsChecked.Value)
        {
            SDTBarcodeScannerPage.StartScan(this,
                                            "YOUR DEVELOPER LICENSE",
                                            SDTBarcodeEngine.SDTBARCODETYPE_ALL_1D,
                                            false,
                                            new SDTBarcodeScannerWP.SDTBarcodeScannerPage.ScanCompletedEventHandler(MainPage_OnScanCompleted));
        }
        else if (rbQrcode.IsChecked.Value)
        {
            SDTBarcodeScannerPage.StartScan(this,
                                            "YOUR DEVELOPER LICENSE",
                                            SDTBarcodeEngine.SDTBARCODETYPE_QRCODE,
                                            false,
                                            new SDTBarcodeScannerWP.SDTBarcodeScannerPage.ScanCompletedEventHandler(MainPage_OnScanCompleted));
        }
        else if (rbDatamatrix.IsChecked.Value)
        {
            SDTBarcodeScannerPage.StartScan(this,
                                            "YOUR DEVELOPER LICENSE",
                                            SDTBarcodeEngine.SDTBARCODETYPE_DATAMATRIX,
                                            false,
                                            new SDTBarcodeScannerWP.SDTBarcodeScannerPage.ScanCompletedEventHandler(MainPage_OnScanCompleted));
        }
    }
                        	
  7. In MainPage.xaml.cs file and the following method to MainPage. This method will be called by SDTBarcodeScanner each time new barcode symbol will be recognized. Set ScanCompletedEventArgs property CanCloseScanner to true to close the Camera preview:
    void MainPage_OnScanCompleted(object sender, ScanCompletedEventArgs e)
    {
        tbResults.Text = "";
        for (int i = 0; i < e.Barcodes.Count; i++)
        {
            tbResults.Text += "[" + e.Barcodes[i].TypeName + "] " + e.Barcodes[i].ValueAsString;
        }
        e.CanCloseScanner = true;
    }
    						
  8. Build and run the application on the Windows phone 8 device. When pressing Scan button you should be able to see camera preview.

Reading Barcodes from C# Windows Mobile Application

The described below sample application sourcecodes are located in C:\Program Files\SD-Toolkit\SDTBarcodeSDKWinCE\Samples\SDTBarcodeARM.Net.Sample.

show screenshots

  1. Install the SDK.
  2. In MS Visual Studio Create new Device Application: New Project->Visual C#->Smart Device->Windows Mobile 6 Professional->Device Application.
  3. In form design view add button. Set Name property to buttonReadFile and Text property to Read File
  4. In form design view add button. Set Name property to buttonCaptureFromCamera and Text property to Capture From Camera
  5. In form design view add TextBox. Set Name property to textBoxResults. We will use it for showing recognition results.
  6. In the solution explorer click right mouse button on Project References and choose Add Reference... Point reference to the SDTBarcodeARM.Net.dll assembly.
  7. In form design view add openFileDialog.
  8. In form design view double click on the buttonReadFile button. It will add OnButtonClick event handler for button openFile. Modify the buttonReadFile_Click handler to be the following:
    private void buttonReadFile_Click(object sender, EventArgs e)
    {
        textBoxResults.Text = "";
        if (this.openFileDialog.ShowDialog() == DialogResult.OK)
        {
            using (SDTBarcodeEngine engine = new SDTBarcodeEngine("YOUR DEVELOPER LICENSE"))
            {
                engine.SetReadInputTypes(SDTBarcodeEngine.SDTBARCODETYPE_ALL_1D);
                engine.SetReadInputDirections(SDTBarcodeEngine.SDTREADDIRECTION_LTR | SDTBarcodeEngine.SDTREADDIRECTION_RTL);
    
                string filePath = openFileDialog.FileName;
                engine.ReadImageFile(filePath, 0);
                for (int resultIdx = 0; resultIdx < engine.GetResultsCount(); resultIdx++)
                {
                    string value = engine.GetResultValue(resultIdx);
                    textBoxResults.Text += value;
                    textBoxResults.Text += "\r\n";
                }
            }
        }
    
    }
                            
  9. In form design view double click on the buttonCaptureFromCamera button. It will add OnButtonClick event handler for button openFile. Modify the buttonCaptureFromCamera_Click handler to be the following:
    private void buttonCaptureFromCamera_Click(object sender, EventArgs e)
    {
        textBoxResults.Text = "";
        using (SDTBarcodeEngine engine = new SDTBarcodeEngine("YOUR DEVELOPER LICENSE"))
        {
            engine.SetReadInputTypes(SDTBarcodeEngine.SDTBARCODETYPE_ALL_1D);
            engine.SetReadInputDirections(SDTBarcodeEngine.SDTREADDIRECTION_LTR | SDTBarcodeEngine.SDTREADDIRECTION_RTL);
    
            if (engine.ReadStillImageFromCamera(1280, 720, "Capture barcode") == 0)
            {
                for (int resultIdx = 0; resultIdx < engine.GetResultsCount(); resultIdx++)
                {
                    string value = engine.GetResultValue(resultIdx);
                    textBoxResults.Text += value;
                    textBoxResults.Text += "\r\n";
                }
            }
       }
    }
                            
  10. Modify Windows CE Emulator properties to map Shared Folder to C:\Program Files\SD-Toolkit\SDTBarcodeSDKWinCE\Images folder. It will reflect sample barcode images included in the SDK.
  11. Build and run the application in on attached device or emulator. Note that capture from camera functionality might not work on emulator.

Reading Barcodes from C++ Windows Mobile Application

The described below sample application source codes are located in C:\Program Files\SD-Toolkit\SDTBarcodeSDKWinCE\Samples\SDTBarcodeSampleARMCpp.

show screenshots

  1. Install the SDK.
  2. In MS Visual Studio Create new Windows CE Project: Visual C++ -> Smart Device -> MFC Smart Device Application.
  3. Select platform SDKs to be added to current project
  4. Choose Application type: Dialog Based
  5. In resource editor modify main application dialog. Add new Button control. Set ID property, to IDC_BUTTON_OPEN_FILE.
  6. Add new EditBox control. Set ID to IDC_EDIT_RESULTS
  7. Click right mouse button on EditBox control and choose Add Variable. Set variable category: Value. Variable name: mResults
  8. Modify newelly created project properties to refer to the SDTBarcode header file and library.

    C/C++ -> General-> Additional Include Directories: C:\Program Files\SD-Toolkit\SDTBarcodeSDKWinCE\Include

    Linker -> Input ->Additional Dependencies: SDTBarcode.lib

    Linker -> General ->Additional Library Directories: C:\Program Files\SD-Toolkit\SDTBarcodeSDKWinCE\lib\armv4 or C:\Program Files\SD-Toolkit\SDTBarcodeSDKWinCE\lib\armv4 i

  9. Select Button. In properties go to Events tab and add Handler for BN_CLICKED command: OnBnClickedButtonOpenFile
  10. Inside OnBnClickedButtonOpenFile() method initialize BarcodeEngine passing your purchased developer license as a parameter.
    void* pReader = SDTCreateBarcodeReaderW(L"your developer license");
  11. Read the image file
    SDTReadImageFileW(pReader, L"\\Storage Card\\Ean13.jpg", 0);
  12. Show results in EditBox
    CString l_sResult;
    
    for(int resultIdx = 0; resultIdx < SDTGetResultsCount(engine); resultIdx++)
    {
       LPCWSTR l_wsValue = SDTGetResultValueW(engine, resultIdx);
       l_sResult.Append(l_wsValue);
       l_sResult.Append(_T("\r\n"));
    }
    
    mResults.Format(_T("%s"), l_sResult);
    UpdateData(FALSE);
    
  13. Destroy Barcode Endine
    SDTDestroyBarcodeReader(pReader);
    pReader = NULL;
    							
  14. Modify Project Properties->Deployment->Additional Files to be: SDTBarcode.dll|X:\Out\DebugArmv4\|%CSIDL_PROGRAM_FILES%\SDTBarcodeARM.Sample.Cpp\|0
  15. Modify Windows CE Emulator properties to map Shared Folder to C:\Program Files\SD-Toolkit\SDTBarcodeSDKWinCE\Images folder. It will reflect sample barcode images included in the SDK.
  16. Build and run the application in on attached device or emulator.