I have an api that returns a byte[], i want to display it as an image in my front application, so i used data url to display the image
this.cardBackgroundService.getImage(event.data.entitlementCertificateNumber, "C003").subscribe(data => {
this.image = data;
console.log(this.image);
});
<img src="data:image/png;base64,{{image}}"/>
the problem is when i display the response of the api in the console, it has this format and it doesn't display as an image
�PNG �Ou���j�000000000H��a
��a````````��a
��a��a```````��a
��a��a````````��a
��a��a```````��a
��a````````�.r�����X��V��QS��\�ۂ���F�`�{lhXnJU��s��iiǯ�O1�;������
To convert a byte array to an image. Create a ByteArrayInputStream object by passing the byte array (that is to be converted) to its constructor. Read the image using the read() method of the ImageIO class (by passing the ByteArrayInputStream objects to it as a parameter).
A byte is 8 bits (binary data). A byte array is an array of bytes (tautology FTW!). You could use a byte array to store a collection of binary data, for example, the contents of a file. The downside to this is that the entire file contents must be loaded into memory.
Any image is merely a sequence of bytes structured in accordance with whatever underlying format used to represent it, eg color data, layers, dimensions, etc. The significance of any byte(s) you see during debugging is entirely dependent upon the native format of the image, eg PNG, TIFF, JPEG, BMP, etc.
RaisedButton( onPressed: () async { List<int> imageBytes = await sampleImage. readAsBytes(); base64Image = base64Encode(imageBytes); print(base64Image);},), SizedBox(height: 30,), Image.
You can display like this, it worked with me.
Import import { DomSanitizer } from '@angular/platform-browser';
Add DomSanitizer
to constructor(private sanitizer: DomSanitizer) { }
this.cardBackgroundService.getImage(event.data.entitlementCertificateNumber, "C003")
.subscribe(data => {
let objectURL = 'data:image/png;base64,' + data;
this.image = this.sanitizer.bypassSecurityTrustUrl(objectURL);
});
In HTML
<img [src]='image' />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With