Browse Source

按图片原始宽高渲染canvas

develop
陈鹏飞 3 years ago
parent
commit
4f8bd0276a
  1. 34
      src/app/system-management/host-config/host-config.component.ts
  2. 7
      src/app/system-management/image-label2/image-label2.component.html
  3. 14
      src/app/system-management/image-label2/image-label2.component.scss
  4. 64
      src/app/system-management/image-label2/image-label2.component.ts
  5. 4
      src/app/system-management/system-management.module.ts
  6. 12
      src/styles.scss

34
src/app/system-management/host-config/host-config.component.ts

@ -10,6 +10,7 @@ import { EditcameraComponent } from './editcamera/editcamera.component';
import { catchError, tap } from 'rxjs/operators';
import { ConfigFormDataService } from 'src/app/service/configFormData.service';
import { SendFileComponent } from './send-file/send-file.component';
import { ImageLabel2Component } from '../image-label2/image-label2.component';
interface Camera {
name: string;
user: string;
@ -185,16 +186,33 @@ export class HostConfigComponent implements OnInit {
this.http.get(`/api/EdgeDevices/${this.hostId}`).subscribe(
{
next: ((data: any) => {
console.log(data)
// let isExist = data.configFiles.find((item, index, arr) => {
// if (item.name == 'source.yaml') {
// console.log("存在", index)
// return item
// }
// })
if (data.configFiles) {
this.router.navigate(['/system/host/camera/imageLabel'], { queryParams: { id: item.id } })
this.isSourceYaml = true
const element = document.documentElement
if(element.requestFullscreen) { //进入全屏
element.requestFullscreen();
}
const modal = this.modal.create({
nzContent: ImageLabel2Component,
nzViewContainerRef: this.viewContainerRef,
nzWidth: 1920,
nzClosable: false,
nzFooter: null,
nzWrapClassName: 'canvasContentBox',
nzBodyStyle: {
'border-radius': '0px',
'padding': '0px',
'margin': '0px',
},
nzComponentParams: {
data: item.id
},
nzOnOk: async () => {
}
});
const instance = modal.getContentComponent();
//this.router.navigate(['/system/host/camera/imageLabel'], { queryParams: { id: item.id } })
} else {
this.isSourceYaml = false
this.message.create('error', '请先下发source.yaml配置');

7
src/app/system-management/image-label2/image-label2.component.html

@ -1,16 +1,15 @@
<div class="box">
<nz-page-header class="site-page-header" (nzBack)="goback()" nzBackIcon nzSubtitle="返回上一页"></nz-page-header>
<div class="canvasBox">
<!-- <nz-page-header class="site-page-header" (nzBack)="goback()" nzBackIcon nzSubtitle="返回上一页"></nz-page-header> -->
<div class="btnbox">
<label class="leftTitle" *ngIf="markType === 0">进出口</label>
<label class="leftTitle" *ngIf="markType === 2">卸油区</label>
<label class="leftTitle" *ngIf="markType === 3">便利店</label>
<!-- <button nz-button nzType="primary">获取标注底图/更新摄像头图片</button> -->
<button nz-button *ngIf="markType === 2" [ngClass]="{selectBtn: isDrawArrow}" (click)="isDrawArrow = true;">箭头方向标绘</button>
<button nz-button *ngIf="markType === 2" [ngClass]="{selectBtn: !isDrawArrow && oilUnloadingArea}" (click)="isDrawArrow = false; oilUnloadingArea = true;">泄油管区域</button>
<button nz-button *ngIf="markType === 2" [ngClass]="{selectBtn: !isDrawArrow && !oilUnloadingArea}" (click)="isDrawArrow = false; oilUnloadingArea = false;">静电接地仪</button>
<button nz-button nzType="primary" (click)="anewgetImg()">重新捕获摄像头图片</button>
<button nz-button nzType="primary" (click)="save()">保存</button>
<button nz-button nzType="primary" nzDanger (click)="clearCanvas()">清空</button>
<button nz-button nzType="primary" nzDanger nz-popconfirm nzPopconfirmTitle="您确定要清空吗?" (nzOnConfirm)="clearCanvas()">清空</button>
</div>
<div class="imgbox">
<div class="content">

14
src/app/system-management/image-label2/image-label2.component.scss

@ -1,4 +1,4 @@
.box {
.canvasBox {
width: 100%;
height: 100%;
background: #fff;
@ -9,7 +9,7 @@
flex-direction: column;
overflow: hidden;
.imgbox{ flex: 1; overflow: hidden; }
canvas{ overflow: hidden; }
canvas{ overflow: hidden; display: block; }
.content,.center{
width: 100%;
height: 100%;
@ -19,17 +19,19 @@
.btnbox {
display: flex;
margin-bottom: 8px;
position: fixed;
left: 1%;
top: 1%;
z-index: 10;
button {
margin-right: 6px;
}
.leftTitle{ line-height: 32px; padding: 0 20px; }
.leftTitle{ line-height: 32px; margin-right: 10px; color: #fff; }
}
.imgbox,
.btnbox {
box-sizing: border-box;
padding: 0 12px;
padding: 0;
}
.selectBtn { background-color: #1890ff; color: #fff; }

64
src/app/system-management/image-label2/image-label2.component.ts

@ -1,7 +1,7 @@
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Component, Input, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalService } from 'ng-zorro-antd/modal';
@Component({
selector: 'app-image-label2',
@ -10,8 +10,9 @@ import { NzMessageService } from 'ng-zorro-antd/message';
})
export class ImageLabel2Component implements OnInit {
constructor(private route: ActivatedRoute, private http: HttpClient, private message: NzMessageService) { }
constructor(private http: HttpClient, private message: NzMessageService, private modal: NzModalService) { }
@Input() data: any; //传递id
camerasData: any; //摄像头Data
imgItem: any; //图片 URL
canvasWidth: number = 0;
@ -39,17 +40,12 @@ export class ImageLabel2Component implements OnInit {
}
anewgetImg() {
this.route.queryParams.subscribe(parmas => {
let params = {
cameraId: parmas.id
cameraId: this.data
}
this.http.put('/api/Cameras/Commands/CaptureImages', '', { params: params }).subscribe(
{
next: (value: Object) => {
// console.log(111, value)
this.message.create('success', '向边缘设备发送请求成功,请过一段时间手动刷新页面!');
},
error: (error: HttpErrorResponse) => {
@ -60,23 +56,20 @@ export class ImageLabel2Component implements OnInit {
}
}
)
})
}
//获取 摄像头图片/标注点位
getImgMarkData() {
let that = this
return new Promise((resolve, reject) => {
that.route.queryParams.subscribe(parmas => {
that.http.get(`/api/Cameras/${parmas.id}`).subscribe((info: any) => {
that.http.get(`/api/Cameras/${that.data}`).subscribe((info: any) => {
info.dimensionedPoints ? info.dimensionedPoints = JSON.parse(info.dimensionedPoints) : null;
that.camerasData = info;
that.markType = info.type;
console.log(that.camerasData, "摄像头数据")
const httpOptions = {
responseType: 'blob' as 'json',
params: { cameraId: parmas.id }
params: { cameraId: that.data }
};
that.http.get(`/api/Cameras/Images`, httpOptions).subscribe({
next: (data) => {
@ -87,7 +80,6 @@ export class ImageLabel2Component implements OnInit {
},
}) //get
}) //get
}) //route
})
}
@ -122,8 +114,13 @@ export class ImageLabel2Component implements OnInit {
}
let paramsData = JSON.parse(JSON.stringify(this.camerasData))
paramsData.dimensionedPoints = JSON.stringify(paramsData.dimensionedPoints)
this.http.put(`/api/Cameras/${this.camerasData.id}`, paramsData).subscribe(data => {
this.http.put(`/api/Cameras/${this.camerasData.id}/DimensionedPoints`, paramsData).subscribe(data => {
this.message.create('success', '保存成功!');
const isFullScreen = document.fullscreenElement
if (document.exitFullscreen && isFullScreen) { //关闭全屏
document.exitFullscreen()
}
this.modal.closeAll();
})
}
@ -149,20 +146,23 @@ export class ImageLabel2Component implements OnInit {
img.src = that.imgItem ? that.imgItem : "../../../assets/images/bgImg.png";
img.onload = () => {
// 等比例缩放图片
var scale = 1;
if (img.width > center.clientWidth || img.height > center.clientHeight) {
let scaleOne = center.clientWidth / img.width;
let scaleTwo = center.clientHeight / img.height;
if (img.width * scaleOne <= center.clientWidth && img.height * scaleOne <= center.clientHeight) {
scale = scaleOne;
} else if (img.width * scaleTwo <= center.clientWidth && img.height * scaleTwo <= center.clientHeight) {
scale = scaleTwo;
} else {
scale = 0.3;
}
}
that.canvasWidth = img.width * scale;
that.canvasHeight = img.height * scale; // 计算等比缩小后图片
// var scale = 1;
// if (img.width > center.clientWidth || img.height > center.clientHeight) {
// let scaleOne = center.clientWidth / img.width;
// let scaleTwo = center.clientHeight / img.height;
// if (img.width * scaleOne <= center.clientWidth && img.height * scaleOne <= center.clientHeight) {
// scale = scaleOne;
// } else if (img.width * scaleTwo <= center.clientWidth && img.height * scaleTwo <= center.clientHeight) {
// scale = scaleTwo;
// } else {
// scale = 0.3;
// }
// }
// that.canvasWidth = img.width * scale;
// that.canvasHeight = img.height * scale; // 计算等比缩小后图片
that.canvasWidth = img.width;
that.canvasHeight = img.height;
console.log(img.width + "*" + img.height)
window.setTimeout(() => { // 加载图片
ctx.drawImage(img, 0, 0, that.canvasWidth, that.canvasHeight);
this.copyCanvas = ctx.getImageData(0, 0, that.canvasWidth, that.canvasHeight)
@ -202,10 +202,6 @@ export class ImageLabel2Component implements OnInit {
//清空画布
clearCanvas() {
let istrue = confirm("清除属于不可逆操作,你是否继续?");
if (!istrue) {
return
}
// 清空标绘箭头
this.arrowPoints = [];
// 清空标绘箭头

4
src/app/system-management/system-management.module.ts

@ -18,6 +18,7 @@ import { NzMessageModule } from 'ng-zorro-antd/message';
import { NzTreeModule } from 'ng-zorro-antd/tree';
import { NzSpinModule } from 'ng-zorro-antd/spin';
import { NzTreeSelectModule } from 'ng-zorro-antd/tree-select';
import { NzPopconfirmModule } from 'ng-zorro-antd/popconfirm';
import { AddorComponent } from './organization/addor/addor.component';
import { EditorComponent } from './organization/editor/editor.component';
import { NzCheckboxModule } from 'ng-zorro-antd/checkbox';
@ -61,7 +62,8 @@ import { ModelComponent } from './condition-monitoring/model/model.component';
NzTreeSelectModule,
NzCheckboxModule,
NzPageHeaderModule,
NzTabsModule
NzTabsModule,
NzPopconfirmModule
],
entryComponents: [AddorComponent, EditorComponent, AddhostComponent, EdithostComponent, AddcameraComponent, EditcameraComponent, SendFileComponent, ModelComponent]

12
src/styles.scss

@ -136,3 +136,15 @@ app-root {
.maxHeightTreeSelect {
max-height: 280px;
}
.canvasContentBox{
.ant-modal {
margin: 0;
padding: 0;
top: 0;
left: 0;
width: 100%;
height: 100%;
max-width: 100%;
}
}
Loading…
Cancel
Save