I'm trying to display an image stored as blob in flutter. I'm using a PHP API to get the image and send it as a base64 string. When the image builds, there is an error that says Failed decoding image. Data is either invalid, or it is encoded using an unsupported format.
Running an xampp server. PHP APIs. Flutter application run using android studio emulator
I am able to display the image on a webpage using a HTML img tag.
Image.memory(base64Decode(imagebase64string));
I expect the image to be shown on the screen
Error says
Failed decoding image. Data is either invalid, or it is encoded using an unsupported format.
You have to remove base64 header manualy:
final stripped =
imagebase64string.replaceFirst(RegExp(r'data:image/[^;]+;base64,'), '');
setState((){
data = base64.decode(stripped);
});
Then you can use data with Image.memory.
Image.memory(data)
Decode it back to a byte array from base64 and use this from dart.ui:
final codec = await ui.instantiateImageCodec(data);
final frame = await codec.getNextFrame();
return frame.image;
Or even simpler, from package:flutter/widgets.dart:
return decodeImageFromList(data);
Related
In my requirement, we need to take digital signature. Digital signature we are receiving it as base 64 string.
Is any way to convert base64 to image in native script.
Official NativeScript Docs for the image-source module have the following
let img: imageSource.ImageSource;
img = imageSource.fromBase64(jpgImageAsBase64String);
Which loads the image in memory from the string. Following you can save the image calling the saveToFile method of image-source instances
let saved = imageSource.saveToFile(path, "png");
if (saved) {
console.log("Image saved successfully!");
}
I'm pulling from an API that gives me back an image in a blob inside the response. I need to display this image somewhere. I tried using react-native-fetch-blob, but its clear that that's a complete mess. I'm guessing there has to be some way I can convert it to base64 so that the image can be displayed.
I am using below method to convert base64 to blob:
var blobData = new Blob([base64Str], {type: 'image/png'});
Now once the image file is created using this Blob:
var imageData = new FormData();
var counter = 1;
imageData.append("file", blobData, 'file1.png');
I do not see the metadata. Is there a way to add the metadata being stripped off the image file?
I download the image file created above and view it in http://metapicz.com/#landing
When viewing original image it shows the location info and other metadata, but when viewing the downloaded image it does not show any metadata.
Figured it out myself, the datatype returned from iOS native (swift) is UIImage which removed the EXIF information and only returns base64 of image data.
Changed the code to return entire image data + metadata at iOS native side.
I am getting an ImagesServiceFailureException exception from the app engine ImagesServiceFactory when specifying JPEG as the output format. It works fine when I specify PNG as the output format. I have only tried this on the app engine development server, not yet on the production server. JPEG is supposed to work according to the app engine docs.
Any idea why this doesn't work? I really don't want to use PNG because of the much greater size images relative to JPEG.
Following is my code:
byte[] sourceData = fetchBlobData(sourceKey);
Image sourceImage = ImagesServiceFactory.makeImage(sourceData);
// Image sourceImage = ImagesServiceFactory.makeImageFromBlob(thumbSourceKey);
ImagesService imagesService = ImagesServiceFactory.getImagesService();
// Create the transform and the new image
Transform resize = ImagesServiceFactory.makeResize(NEW_WIDTH,NEW_HEIGHT);
Image newImage = imagesService.applyTransform(resize, sourceImage, OutputEncoding.JPEG);
When I output as JPEG, I get the following exception:
com.google.appengine.api.images.ImagesServiceFailureException: Failed to encode image
Any help would be greatly appreciated.
In Google AppEngine I want to transform a boolean[] into an image then serve the image. I want the boolean[] to be transformed to black and white pixels. I can see that AppEngine provides
import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
// ...
byte[] imageData; // ...
ImagesService imagesService = ImagesServiceFactory.getImagesService();
Image image = ImagesServiceFactory.makeImage(imageData);
but I don't know what the format of the byte[] imageData should be, i.e., how to transform the boolean[] to byte[]
And once I have this image, how can the client get it?
The Image service API accepts data in any of the supported image file formats. According to this page, these formats include "JPEG, PNG, WEBP, GIF (including animated GIF), BMP, TIFF and ICO". The Image service does not provide a way to create new image data from scratch. But you can use a graphics library to produce the image in one of the accepted data formats, then use the service to convert it to another format, or transform it. Of course, depending on the graphics library, you may be able to get the final image directly from the library, and not use the service.
To serve an image, just set the appropriate Content-Type header for the data format you're using, then write the bytes of the image data to the response's output stream:
// byte[] pngData = ...
resp.addHeader("Content-Type", "image/png");
resp.getOutputStream().write(pngData);
If you want to try generating the image data without a library, the BMP format might be easiest. You can use the Images service to convert this to PNG.