如何将Angle 4 Web App连接到移动相机?

时间:2019-03-28 03:48:27

标签: angular html5 typescript asp.net-web-api

这里在Visual Studio 2015(更新3)中使用angular 4 Web API。 现在,我想在移动设备中使用条形码搜索项目。如何完成此过程,请注意,这是一个有经验的初学者,所以请为我提供这个解决方案的帮助我,我搜索了很多网站,但我无法理解。任何人都可以帮助我找到解决方案。 (TS文件和HTML文件)

2 个答案:

答案 0 :(得分:2)

在TS文件中输入功能

 constructor(private zone: NgZone){
  window.angularComponentReference = {
             zone: this.zone,
            componentFn: (searchcontent: any) => this.scannerOutput(searchcontent),       
            component: this,
        };



//  And write the function

    barcode() {
    if (typeof Android !== "undefined" && Android !== null) {
        Android.openScanner();
    }
    else {
        alert("sorry no item");
    }
}

还有index.html

        function scannerOutput(searchcontent) {
        window.angularComponentReference.zone.run(() =>
        { window.angularComponentReference.componentFn(searchcontent); });
    }

答案 1 :(得分:1)

我不确定是否可以,但是您可以尝试@zxing:

第1步-安装npm软件包:

npm install --save @zxing/library @zxing/ngx-scanner

第2步-添加到您的app.module.ts:

import { ZXingScannerModule } from '@angular/forms';

注意:请记住要在“导入” 部分

中添加此库

第3步-实现.component.ts:

import { Component, OnInit, ViewChild } from '@angular/core';
import { ZXingScannerComponent } from '@zxing/ngx-scanner';
import { Result } from '@zxing/library';
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

    @ViewChild('scanner')
    scanner: ZXingScannerComponent;
    hasDevices: boolean;
    hasPermission: boolean;
    qrResultString: string;
    qrResult: Result;
    availableDevices: MediaDeviceInfo[];
    currentDevice: MediaDeviceInfo;

    ngOnInit(): void {
        this.scanner.camerasFound.subscribe((devices: MediaDeviceInfo[]) => {
        this.hasDevices = true;
        this.availableDevices = devices;
    });
    this.scanner.camerasNotFound.subscribe(() => this.hasDevices = false);
    this.scanner.scanComplete.subscribe((result: Result) => this.qrResult = result);
    this.scanner.permissionResponse.subscribe((perm: boolean) => this.hasPermission = perm);
    }

    displayCameras(cameras: MediaDeviceInfo[]) {
        this.availableDevices = cameras;
    }

    handleQrCodeResult(resultString: string) {
        this.qrResultString = resultString;
    }

    onDeviceSelectChange(selectedValue: string) {
        this.currentDevice = this.scanner.getDeviceById(selectedValue);
    }
}

步骤4-实施.component.html

<div style="width: 50%" class="scanner-shell" [hidden]="!hasDevices">
<header>
    <select (change)="onDeviceSelectChange($event.target.value)">
        <option value="" [selected]="!currentDevice">No Device Selected</option>
        <option *ngFor="let device of availableDevices" [value]="device.deviceId"
        [selected]="currentDevice && device.deviceId === currentDevice.deviceId">
            {{ device.label }}
        </option>
    </select>
</header>
<zxing-scanner #scanner start="true" [device]="currentDevice"
(scanSuccess)="handleQrCodeResult($event)" 
[formats]="['QR_CODE', 'EAN_13','CODE_128', 'DATA_MATRIX']"></zxing-scanner>
    <section class="results" *ngIf="qrResultString">
    <small>Result: </small>
    <strong>{{ qrResultString }}</strong>
</section>

结果:

因此,一旦您在任何设备上打开此组件,浏览器就会要求您访问设备相机。如果您愿意使用它,则应该能够从下拉菜单中选择摄像头,然后,如果使用它扫描Qr码或条形码,则应该在视图上看到其结果。

注意:您必须允许“系统设置”中的应用程序使用相机。对于Windows 10,您可以在相机隐私设置-> 允许应用访问您的相机

中进行操作