// 导入ImageCropper模块 import { ImageCropperModule } from 'ng2-img-cropper';
@Component({ selector: 'app-image-upload', templateUrl: './image-upload.component.html', styleUrls: ['./image-upload.component.scss'] }) export class ImageUploadComponent implements OnInit {
// 图像裁剪器配置 private cropperConfig: any = { aspectRatio: 1, crop: function(data: any) { }, showCentered?: boolean = true };
// 图像裁剪器组件实例 @ViewChild('cropper', { static: false }) private cropper: ImageCropperComponent;
// 裁剪后的图像数据 public croppedImageData: string;
constructor() { }
ngOnInit(): void { }
// 上传图像文件并加载到裁剪器中 public handleFileSelect(event: any) { const image = new Image(); const file: File = event.target.files[0]; const reader = new FileReader(); reader.onloadend = (loadEvent: any) => { image.src = loadEvent.target.result; this.cropper.setImage(image); }; reader.readAsDataURL(file); }
// 保存裁剪后的图像数据 public saveCroppedImage() { this.cropper.crop(); this.croppedImageData = this.cropper.image.imageDataUrl; console.log(this.croppedImageData); }
// 重置裁剪器 public resetCropper() { this.cropper.reset(); this.croppedImageData = null; }
}
在HTML文件
上一篇:Angular中的图像标注
下一篇:Angular中的图像放大器