Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - Display byte array as image

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�;������

like image 904
Aymen Kanzari Avatar asked May 03 '19 10:05

Aymen Kanzari


People also ask

How can I get image from byte array?

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).

How does a byte array work?

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.

How is an image stored in a byte array?

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.

How do you turn an image into a byte in flutter?

RaisedButton( onPressed: () async { List<int> imageBytes = await sampleImage. readAsBytes(); base64Image = base64Encode(imageBytes); print(base64Image);},), SizedBox(height: 30,), Image.


1 Answers

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' />
like image 112
Hien Nguyen Avatar answered Sep 25 '22 20:09

Hien Nguyen