How to get `Uint8List` from `ImmutableBuffer`? - flutter

The new Flutter 3.3 deprecates the method load of class ImageProvider and now some my code became not working because load had a parameter bytes of type Uit8List but loadBuffer has a parameter buffer of ImmutableBuffer.
I used Uint8List to decode image using image package
final bytes = img.decodeImage(bytes); // bytes is instance of Uit8List
The complete code can be read from the article Remove a color from an image in Flutter starting from section 4. Decode.
And now I need to convert ImmutableBuffer to Uint8List. Does someone know how to do this?

Related

Flutter camera package, how can I rotate the image?

My code is simle;
final XFile image=await cameraController!.takePicture();
takenImage=await image.readAsBytes();
and when I am just using this two line, I can see the image with Image.memory(_vm.takenImage!, fit: BoxFit.fitWidth)
But i need to change picture rotation,
for this reason, I am using image package https://pub.dev/packages/image
so, why I am getting invalid image data after just these lines;
final XFile image=await cameraController!.takePicture();
takenImage=await image.readAsBytes();
img.Image? decodedImage=img.decodeImage(takenImage!);
takenImage=decodedImage!.getBytes();
If I delete the last two sentence it's working correctlye but if I add the last lines I am getting Exceltion: invalid image data, but why ? I mean .getBytes returning Uint8List, takenImage type is Uint8List? ...
if I can convert like my second code review, I can use the; img.copyRotate(originalImage!, 90).getBytes(); function, so please help me for understand.
For my problem related image type cause as you can see on question I wasn't encode my image with any type, so the solution and working code now can seen on below;
final XFile image=await cameraController!.takePicture();
img.Image? _capturedImage=img.decodeImage(await image.readAsBytes());
_capturedImage=img.copyRotate(_capturedImage!, 90);
takenImage=Uint8List.fromList(img.encodePng(_capturedImage));

Reduce size image from bytes Uint8List

I am using the file_picker package to import images before uploading them to Firebase Storage, because it allows me to choose the formats what is not possible with image_picker.
I would like to be able to reduce the size of these images to reduce their weight. The flutter_image_compress package allows to compress images but it is not available for Flutter Web.
How can I reduce the size of an image imported in Flutter Web knowing that the format is Uint8List?
You can use the archive package to compress any data. The following is an example function taking Uint8List and compressing it:
Uint8List compress(Uint8List input) {
final encoder = ZLibEncoder();
return encoder.encode(input) as Uint8List;
}
You'll want to do this in a separate isolate since this could noticeably block your UI, but this won't make any difference if your application is web-only.
if you know the image type you can convert it to jpeg and then compress it using the flutter image package.
import 'package:image/image.dart' as img;
Future<List<int>> _pngToJpg(Uint8List pngBytes) async {
final pngImage = img.decodePng(pngBytes);
final pngImageResized = img.copyResize(pngImage!, width: 800);
return img.encodeJpg(pngImageResized, quality: 90);
}
you can use any other decoder depending on your image type. Expect a freeze in UI. You can use compute to minimize it.

How to convert Audio bytes to String in flutter

I'm getting List<int> as RAW data from recorder plugin for Android and iOS, I want to display Actual text from the bytes, The data is stream of system mic.
Any way to get text from bytes?
Raw data detail:
SampleRate: 44100,
ChannelConfig: MONO-16,
AudioSource: SYSTEM-MIC
Note: I'm already using SpeechToText plugin and aware about it, But I fill that at some-point which is dropping words hence I want to try something else.
Any help will be appreciated.
import 'package:mime/mime.dart';
String? lookupMimeType(String path, {List<int>? headerBytes})
You can use ı think.
var mimeString = lookupMimeType();İf you fill the parameter it works. You can use directly your audio file's path.

How to add markers on the map

I'm using the Flutter SDK Version 4.3.1.0.
I would like to display markers on the map and I found that this would be possible via:
final String positionMarkerPath = 'assets/markers/marker.png';
mapScene.addMapMarker(
MapMarker(
geoCoordinates,
MapImage.withFilePathAndWidthAndHeight(positionMarkerPath, 32, 32)));
When using this method I constantly get errors
E/CL_geoviz( 9632): [ERROR] CL_geoviz - Can't find image 'file:///assets/markers/marker.png' in asset repository.
E/CL_geoviz( 9632): [ERROR] CL_geoviz - Can't load image file 'file:///assets/markers/marker.png' to memory stream.
Now, the method MapImage.withFilePathAndWidthAndHeight is documented as follows in the SDK:
Creates a new map image from the provided path to the SVG Tiny image which is weird because I thought that Flutter doesn't even support SVG out of the box. Could that be an issue? Or what am I doing wrong here?
I tried using an SVG, Vector Drawable and a png, tried fully qualifying the path, nothing works
The documentation for MapImage.withFilePathAndWidthAndHeight says that it is only for SVG Tiny format. But it currently does not seem to work as the HERE SDK for Flutter is still in Beta state.
You can instead load and add PNG files as marker like this:
ByteData data = await rootBundle.load('assets/some_image.png');
MapImage image = MapImage.withPixelDataAndImageFormat(Uint8List.view(data.buffer), ImageFormat.png);
MapMarker marker = MapMarker(geoCoordinates, image);
hereMapController.mapScene.addMapMarker(marker);
The assets directory needs to be specified in the pubspec.yaml.

Encoded string is not working in swift

In my swift App, I need to convert Image as String, then I need to send to Server , i.e POST. Then, if I need to GET, I dont want to decode that. Response having Image file name and path.
ONline Converter:
If I convert the sample image through online converter, HERE. I am getting following string. If I pass this string to API, I can get image successfully.
Output:
iVBORw0KGgoAAAANSUhEUgAAAJAAAACQCAYAAADnRuK4AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR.... .....CIBhIIgQRCIIEQSCAQAgmEQAIhkEAIJBAIgQRCIIEQSCAEEgiEQAIhkEAIJCgN/F+AAQAXoGXl+IGgwwAAAABJRU5ErkJggg==
Through My Coding
If I convert the sample image through Coding, I am getting following string. If I pass this string to API, Successfully adding. But, I unable to view that image manually in browser too.
More Over,, Two Output Strings are not same. I dont know why? Kindly guide me, how to solve this?
Code:
var image : UIImage = UIImage(named: "home_recharge.png")!
var imageData = UIImagePNGRepresentation(image)
let base64String = imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.allZeros)
println("base64String \(base64String)")
Output:
iVBORw0KGgoAAAANSUhEUgAAAJAAAACQCAYAAADnRuK4AAAAAXNSR0IArs4c6QAAABxpRE9UAAAAAgAAAAAAAABIAAAAKAAAAEgAAABIAAAKygzM7MwAAAqWSURBVHgB7FwJ0JZDHC9yhJTkLuRMjuQ+opAr5zjGqAwZx...... .......FlIs5ASSDNQLA7EMtYMpBlICaQZKBYHYhlrBtIMpATSDBSLA7GMNQNpBlICaQaKxYFYxpqBNAMpgTQDxeJALGPNQJqBlECagWJxIJaxZiDNQP8B+6MkTad9NtcAAAAASUVORK5CYII=
You use png image representation but server saves the image with .jpg extension so browser tries to read image as JPEG file and fails.
You should replace UIImagePNGRepresentation with UIImageJPEGRepresentation or save image with .png extension.
The reason why browser can open online converter result image despite it has wrong extension is possibly that UIImagePNGRepresentation strips the image of its meta data and online converter not so browser can not figure the real type of image with wrong extension without meta data.