如何将角度4 web应用程序连接到移动相机?

问题描述 投票:0回答:1

这是在visual studio 2015中使用angular 4 web API(更新3)。现在我想在移动设备中使用条形码搜索项目。如何完成这个过程请注意,我是一个角度的初学者所以请帮助我这个解决方案我搜索这么多的网站,但我无法得到或理解这个想法。有人请帮我找到解决方案。 (TS文件和Html文件)

angular html5 typescript asp.net-web-api
1个回答
2
投票

我不确定它会起作用,但你可以试试@zxing:

第1步 - 安装npm包:

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

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

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

注意:请记住在“import”部分添加此库

第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,您可以在相机隐私设置 - >允许应用访问您的相机中执行此操作


0
投票

在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); });
    }
© www.soinside.com 2019 - 2024. All rights reserved.