简体   繁体   English

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

[英]How to connect angular 4 web app to mobile camera?

Here am using angular 4 web API in visual studio 2015 (updated 3). 这里在Visual Studio 2015(更新3)中使用angular 4 Web API。 Now i want to search an item using bar code in mobile devices . 现在,我想在移动设备中使用条形码搜索项目。 how to be done this process please note that am a beginner in angular so please help me for this solution i search so many site but i could not get or understand the idea. 如何完成此过程,请注意,这是一个有经验的初学者,所以请为我提供这个解决方案的帮助我,我搜索了很多网站,但我无法理解。 anyone please help me for finding the solution. 任何人都可以帮助我找到解决方案。 (TS file and Html file) (TS文件和HTML文件)

I am not sure would it work, but you can try @zxing: 我不确定是否可以,但是您可以尝试@zxing:

Step 1 - Install npm packages: 第1步-安装npm软件包:

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

Step 2 - Add to your app.module.ts: 第2步-添加到您的app.module.ts中:

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

Note: remember to add this library in 'import' section 注意:请记住在“导入”部分中添加此库

Step 3 - Implement .component.ts: 第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);
    }
}

Step 4 - Implement .component.html 第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>

Result: 结果:

As a result, once you open this component on any device, the browser will ask you for access to your device camera. 因此,一旦您在任何设备上打开该组件,浏览器就会要求您访问设备相机。 If you will grand it, you should be able to pick camera from dropdown and then, if you scan with it Qr code or bar code, you should see its outcome on the view. 如果您愿意使用它,则应该能够从下拉菜单中选择摄像头,然后,如果使用它扫描Qr码或条形码,则应该在视图上看到其结果。

Note: You have to allow camera to be used by applications in your System Settings. 注意:您必须允许系统设置中的应用程序使用相机。 For Windows 10, you can do it in Camera privacy settings -> Allow apps to access your camera 对于Windows 10,您可以在“ 相机隐私设置” ->“ 允许应用访问您的相机”中进行操作

Enter the function in TS file 在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");
    }
}

And index.html 还有index.html

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM