Good day,
Is there a way I could convert JPEG2000 base64 data to PNG data in Angular so I could display it on the browser? Here's a test component to give you an idea of what I need:
my-component.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'dha-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.scss']
})
export class MyComponentComponent implements OnInit {
imageData: string;
constructor() {}
ngOnInit(): void {
const testJ2KImage = '/0//UQA...';
this.imageData = this.convertJ2kToPng(testJ2KImage);
}
convertJ2kToPng(base64Data: string) {
// Convert base 64 data from JPEG2000 to PNG
const newPngImageData = 'iVBOR...';
return newPngImageData;
}
}
my-component.component.html
<img [src]="'data:image/png;base64,' + imageData" class="img img-responsive" />
Normally it's done via a backend call, however, it's possible (albeit tricky) to do so in the browser.
First of all, let's decode the input image. There doesn't seem to be a usable NPM package that does it in the browser, so we'll use j2k.js and load it directly:
Then let's convert the decoded input image to PNG. We'll use the fast-png package for that:
StackBlitz
Alternatively, this would be the backend way:
I decided to set up a simple Express backend that uses
jpeg2000andpngjsfor the image conversion:StackBlitz -- please navigate to https://angularwebcontainertemplateddn-livb--4200--95b70c8d.local-credentialless.webcontainer.io when both applications are started.