i am using image picker package in flutter , so i want to mention the max size of image selected should be 5 MB, in the package there is a parameter named imagequality which take 0-100, but don't equivalent for 5 mb
getImageFromGallery() async {
try {
var pickedfiles = await imagePicker.pickMultiImage(imageQuality: 5);
if (pickedfiles.isNotEmpty) {
for (int i = 0; i < pickedfiles.length; i++) {
previewList.add(pickedfiles[i].path);
}
setState(() {});
}
} catch (e) {
log(e.toString());
}
}
Try this, I hope this helps
selectImagesFromImagePicker() async{
// e.g
final knowSize = await defindSize(fileSelected, 2);
if (double.parse(knowSize.toString()) > 5.0) {
SomeSnackBar("File too large", "Limit is 5 mb below");
} else {
log("$knowSize mb", name: "FILE SIZE");
filesChosen.add(File(x!));
}
}
// Function to know the size of a certain file
defindSize(String? path, int decimals) {
final file = File(path!);
int bytes = file.lengthSync();
log("lengthSync $bytes", name: "FILE SIZE");
double sizeMb = bytes / (1000 * 1000);
// the return is mb default
return sizeMb.toStringAsFixed(decimals);
// below if you wanted to return suffix just uncomment them
// const suffixes = ["b", "kb", "mb", "gb", "tb"];
// var i = (m.log(bytes) / m.log(1024)).floor();
// return ((bytes / m.pow(1024, i)).toStringAsFixed(decimals)) + suffixes[i];
}
Related
I try to use flutter libserialport bull to implement Modbus RTU, however after calling the function it doesn't work.
List<int> receivedData = await readRegisters(0xFE, 0x0000, 0x001A);
print('Data: $receivedData');
When I call, nothing prints. However, it works if I use a private list and setState()!
await reader.stream.listen((data) {
print('stream : $data');
setState(() {
response!.addAll(data);
});
}).asFuture();
Output:
enter image description here
But after await as I called, I do want it to return for the instance variable. how I can solve it
Future<List<int>> readRegisters(int slaveID, int startAddress, int numberOfRegisters) async {
// construct the Modbus Read Holding Registers command
List<int> data = [slaveID,
0x03,
startAddress >> 8,
startAddress & 0xFF,
numberOfRegisters >> 8,
numberOfRegisters & 0xFF ];
int calculatedChecksum = calculateModbusChecksum(data);
data.add(calculatedChecksum & 0xFF);
data.add(calculatedChecksum >> 8);
// write the command to the serial port
port.write(command(data));
SerialPortReader reader = SerialPortReader(port, timeout: 1000);
List<int> response = [];
int bytesReceived = 0;
while (bytesReceived < numberOfRegisters) {
await reader.stream.listen((receivedData) {
print('received : $receivedData');
if (receivedData.isNotEmpty) {
response.addAll(receivedData);
bytesReceived += receivedData.length;
}
}).asFuture(response) ;
}
return response;
}
List<int> receivedData = await readRegisters(0xFE, 0x0000, 0x001A) as List<int>; Future.delayed(const Duration(seconds: 1),()=> print('Data: $receivedData'));
but they not return anything
I have a file that is larger than 512 bytes to send to an esp32. I'm sending "withoutResponse: false", the flutter_blue library does the split according to the mtu size without problems, but when it reaches 512 bytes it returns an error to write in characteristic. To solve this I have a function that splits the file and writes each 512 bytes.
Esp32 can send me files larger than 512 without doing anything. Can I send larger files without splitting?
Example of code or library that makes this possible
ok, I decided to switch to the flutter_reactive_ble library, and I developed code similar to this one. Basically I split the file into smaller packets of 4096 bytes (this was defined in my communication protocol with esp32), then I call the function that sends and it splits again the file into packets the size of mtu less 19 bytes that are from the header. In the last packet I "writeWithResponse" and wait for the response, if it responds ok, increment and send the next package and repit the processs until the end of file.
late StreamSubscription<List<int>>? subscribeStream;
List<int> bytesOfFile = [];
int _size = 0;
int _increment = 0;
List<int> _returnOfSubscribe = [];
int _indexOfUploadController = 0;
Future<void> subscribeCharacteristic() async {
subscribeStream = widget
.subscribeToCharacteristic(widget.rcharacteristic)
.listen((event) {
debugPrint("resposta${hexToString(event)}");
setState(() {
_returnOfSubscribe = event;
if (_returnOfSubscribe != 'code of confirm reception file') {
debugPrint('err');
} else {
_increment++;
actualize();
}
});
});
}
void actualize() async {
int splitSize = 4096;
Iterable<int> s;
int n = (bytesOfFile.length / splitSize).ceil();
//if is the last split
if (_increment >= n) {
if (_returnOfSubscribe == 'code of confirm end of file') {
debugPrint("success");
} else {
debugPrint("err");
}
return;
}
if ((_size + splitSize) < bytesOfFile.length) {
s = bytesOfFile.getRange(_size, _size + splitSize);
} else {
s = bytesOfFile.getRange(_size, bytesOfFile.length);
}
await writeLongData(s.toList());
_size += splitSize;
}
writeLongData(List<int> data) async {
int splitSize = mtuNotifier.value - 19;
Iterable<int> s;
int size = 0;
int n = (data.length / splitSize).ceil();
for (int i = 0; i < n; i++) {
if ((size + splitSize) < data.length) {
s = data.getRange(size, size + splitSize);
} else {
s = data.getRange(size, data.length);
}
try {
if ((size + splitSize) < data.length) {
await widget.writeWithoutResponse(widget.wcharacteristic, s.toList());
} else {
//if the last, write with response
await widget.writeWithResponse(widget.wcharacteristic, s.toList());
}
} catch (e) {
debugPrint('$e');
return;
}
size += splitSize;
}
}
Here is my code
void main() {
String phoneNumber = '123456789';
String formattedPhoneNumber = phoneNumber.replaceFirst("(\d{3})(\d{3})(\d+)", "(\$1) \$2-\$3");
print('Formatted number ${formattedPhoneNumber}');
}
Output:
Formatted number 123456789
I want output as Formatted number (123) 456-6789
Try this
print('1234567890'.replaceAllMapped(RegExp(r'(\d{3})(\d{3})(\d+)'), (Match m) => "(${m[1]}) ${m[2]}-${m[3]}"));
Create a custom Masked class
import 'package:flutter/material.dart';
class MaskedTextController extends TextEditingController {
MaskedTextController({String text, this.mask, Map<String, RegExp> translator})
: super(text: text) {
this.translator = translator ?? MaskedTextController.getDefaultTranslator();
this.addListener(() {
var previous = this._lastUpdatedText;
if (this.beforeChange(previous, this.text)) {
this.updateText(this.text);
this.afterChange(previous, this.text);
} else {
this.updateText(this._lastUpdatedText);
}
});
this.updateText(this.text);
}
String mask;
Map<String, RegExp> translator;
Function afterChange = (String previous, String next) {};
Function beforeChange = (String previous, String next) {
return true;
};
String _lastUpdatedText = '';
void updateText(String text) {
if(text != null){
this.text = this._applyMask(this.mask, text);
}
else {
this.text = '';
}
this._lastUpdatedText = this.text;
}
void updateMask(String mask, {bool moveCursorToEnd = true}) {
this.mask = mask;
this.updateText(this.text);
if (moveCursorToEnd) {
this.moveCursorToEnd();
}
}
void moveCursorToEnd() {
var text = this._lastUpdatedText;
this.selection = new TextSelection.fromPosition(
new TextPosition(offset: (text ?? '').length));
}
#override
void set text(String newText) {
if (super.text != newText) {
super.text = newText;
this.moveCursorToEnd();
}
}
static Map<String, RegExp> getDefaultTranslator() {
return {
'A': new RegExp(r'[A-Za-z]'),
'0': new RegExp(r'[0-9]'),
'#': new RegExp(r'[A-Za-z0-9]'),
'*': new RegExp(r'.*')
};
}
String _applyMask(String mask, String value) {
String result = '';
var maskCharIndex = 0;
var valueCharIndex = 0;
while (true) {
// if mask is ended, break.
if (maskCharIndex == mask.length) {
break;
}
// if value is ended, break.
if (valueCharIndex == value.length) {
break;
}
var maskChar = mask[maskCharIndex];
var valueChar = value[valueCharIndex];
// value equals mask, just set
if (maskChar == valueChar) {
result += maskChar;
valueCharIndex += 1;
maskCharIndex += 1;
continue;
}
// apply translator if match
if (this.translator.containsKey(maskChar)) {
if (this.translator[maskChar].hasMatch(valueChar)) {
result += valueChar;
maskCharIndex += 1;
}
valueCharIndex += 1;
continue;
}
// not masked value, fixed char on mask
result += maskChar;
maskCharIndex += 1;
continue;
}
return result;
}
}
Now call it in your main dart file
var maskedController = MaskedTextController(mask: '(000) 000-0000');
TextField(
controller: maskedController,
style: Styles.textNormalStyle,
maxLines: 1,
),
This solution work for your this specific Question and scenario.
you can achieve using following code.
String formattedPhoneNumber = "(" + phoneNumber.substring(0,3) + ") " +
phoneNumber.substring(3,6) + "-" +phoneNumber.substring(6,phoneNumber.length);
Ricardo pointed to a great library but his answer is half right. Besides the intl_phone_number_input you need to get libphonenumber_plugin installed as well.
intl_phone_number_input: ^0.7.0+2
libphonenumber_plugin:
The method getRegionInfoFromPhoneNumber "discovers" what country the number is from eg +55... it would interpret as it's from Brasil and proceed to format the phone number accordingly. You can also explicitly tell from where the phone number is from passing the country's acronym into the method eg. await PhoneNumber.getRegionInfoFromPhoneNumber(phone, "US"); It'll disregard a country code if it doesn't fit the number you're entering.
String phone = "+19795555555";
PhoneNumber number =
await PhoneNumber.getRegionInfoFromPhoneNumber(phone);
String formattedNumber = await PhoneNumberUtil.formatAsYouType(
number.phoneNumber!,
number.isoCode!,
);
print(formattedNumber); // -> prints: '+1 979-555-5555'
Also you can use: https://pub.dev/packages/intl_phone_number_input/example
String phoneNumber = '+234 500 500 5005';
PhoneNumber number = await PhoneNumber.getRegionInfoFromPhoneNumber(phoneNumber);
String parsableNumber = number.parseNumber();
`controller reference`.text = parsableNumber
I'm develop a pulse counter(coin counter) in raspberry pi with windows 10 iot core, and i need to count pulses that have a interval time of 25 miliseconds like this:
0,05€ - 1 pulse
0,10€ - 2 pulses
0,20€ - 4 pulses
0,50€ - 10 pulses
1€ - 20 pulses
2€ - 40 pulses
like this image: pulses
I need to print the number of pulses(to have the value inserted) when the interval time is diferent of 25 miliseconds.
I have this code:
public MainPage()
{
this.InitializeComponent();
InitGPIO();
}
private void InitGPIO()
{
var gpio = GpioController.GetDefault();
if (gpio == null)
{
GpioStatus.Text = "There is no GPIO controller on this device.";
}
coinPin = gpio.OpenPin(coin_Pin);
if (coinPin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
{
coinPin.SetDriveMode(GpioPinDriveMode.InputPullUp);
} else
{
coinPin.SetDriveMode(GpioPinDriveMode.Input);
}
coinPin.DebounceTimeout = TimeSpan.FromMilliseconds(25);
coinPin.ValueChanged += coinPin_ValueChanged;
GpioStatus.Text = "GPIO pins initialized correctly.";
}
private void coinPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
{
if (e.Edge == GpioPinEdge.FallingEdge)
{
counter++;
}
var task = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
if (e.Edge == GpioPinEdge.FallingEdge)
{
//counter++;
var time = PulseIn(coinPin, e.Edge);
value = (counter * 5);
value100 = value / 100;
//money.Text = "Eur: " + (decimal)value100 + " €";
if (time != 25)
{
money.Text = "Eur: " + (decimal)value100 + " €";
GpioStatus.Text = "" + time;
} else
{
GpioStatus.IsEnabled = false;
}
//GpioStatus.Text = "" + time + "";
} else
{
///GpioStatus.Text = "" + coinPin.DebounceTimeout;
}
});
}
private double PulseIn(GpioPin pin, GpioPinEdge edge)
{
var sw = new Stopwatch();
while (edge.Equals(GpioPinEdge.RisingEdge))
{
//sw.Start();
}
sw.Start();
if (!edge.Equals(GpioPinEdge.RisingEdge))
{
//sw.Stop();
}
sw.Stop();
return sw.Elapsed.TotalMilliseconds;
}
private const int coin_Pin = 24;
private int counter = 0;
private double value = 0;
private double value100 = 0;
private GpioPin coinPin;
Can you help me please?
Thank you very much.
From your code,
sw.Start();
if (!edge.Equals(GpioPinEdge.RisingEdge))
{
//sw.Stop();
}
sw.Stop();
it actually measures the execute time of the if statement that between sw.Start() and sw.Stop(). This does not make sense. Record the watch.Elapsed.TotalMilliseconds when the falling edge arrived and restart the stopwatch to measure the pulse interval time. To do this, I add the following two code lines under counter++, remove var time = PulseIn(coinPin, e.Edge) and use timeinterval in GpioStatus.Text = "" + timeinterval instead.
timeinterval = watch.Elapsed.TotalMilliseconds;
watch.Restart();
The following is complete code piece:
private Stopwatch watch = new Stopwatch();
private void coinPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
{
double timeinterval = 0;
if (e.Edge == GpioPinEdge.FallingEdge)
{
counter++;
timeinterval = watch.Elapsed.TotalMilliseconds;
watch.Restart();
}
var task = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
if (e.Edge == GpioPinEdge.FallingEdge)
{
//var time = PulseIn(coinPin, e.Edge);
value = (counter * 5);
value100 = value / 100;
if (timeinterval != 25)
{
money.Text = "Eur: " + (decimal)value100 + " €";
GpioStatus.Text = "" + timeinterval;
}
//...
}
});
}
You can try above code piece to see if it meets your accuracy requirement.
Note: It is not suitable to measure hardware pulse interval in software level because the software jitter is always unpredictable.
I'm building an App with actionscript 3.0 in my Flash builder. This is a followup question to this question, It works but when I take the picture, the image comes out rotated to the left. how can I check which way the user is holding the phone? and then what code do I use to rotate the image to it's corresponding place?
Thanks in advanced!
EDIT:
I'm using this code to rotate the image, but it seems to only rotate the image being displayed not the image file, any ideas?
var mat:Matrix = new Matrix();
mat.translate(-W/2, -H/2);
mat.rotate(Math.PI/2);
mat.translate(+W/2, +H/2);
mat.concat(myObj.transform.matrix);
myObj.transform.matrix = mat;
~Myy
You can't get stage.orientation data when you have the cameraUI view active, so here's how i solved the same issue, please note that this controller has many reusable functions, i first get the raw image data from media promise (this raw data has EXIF data that i will read to get image orientation from the camera) i then convert this byteArray to BitmapData and then resize and scale as i need. One thing to note is that if you convert the raw image data to Bitmap data you will lose EXIF data so read it before you modify the image, hope this helps and i posted it here late i know but there's no other solution in the web and maybe someone will need this sometime. Cheers
SnapshotController.as
package controllers
{
import flash.display.BitmapData;
import flash.display.JPEGEncoderOptions;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IEventDispatcher;
import flash.events.MediaEvent;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.media.CameraUI;
import flash.media.MediaPromise;
import flash.media.MediaType;
import flash.utils.ByteArray;
import flash.utils.IDataInput;
import mx.utils.Base64Encoder;
import classes.APIupload;
public class SnapshotController
{
private var cameraUI:CameraUI = new CameraUI();
private var dataSource:IDataInput;
private var tempDir:File = new File();
private var imageOrientation:int = 0 ;
public function SnapshotController()
{
}
public function LaunchCameraUI():void
{
if( CameraUI.isSupported ) {
trace( "Initializing camera..." );
cameraUI.addEventListener( MediaEvent.COMPLETE, imageSelected );
cameraUI.launch( MediaType.IMAGE );
} else {
trace( "CameraUI is not supported.");
}
}
private function imageSelected( event:MediaEvent ):void {
trace( "Media selected..." );
var imagePromise:MediaPromise = event.data;
dataSource = imagePromise.open();
if( imagePromise.isAsync ) {
trace( "Asynchronous media promise." );
var eventSource:IEventDispatcher = dataSource as IEventDispatcher;
eventSource.addEventListener( Event.COMPLETE, onMediaLoaded );
} else {
trace( "Synchronous media promise." );
readMediaData();
}
}
private function onMediaLoaded( event:Event ):void {
trace("Media load complete");
readMediaData();
}
private function readMediaData():void {
var imageBytes:ByteArray = new ByteArray();
dataSource.readBytes( imageBytes );
imageOrientation = getOrientation(imageBytes);
//saveImageFile(imageBytes);
//Saving this byteArray will save a big file with the EXIF in it.
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, onMediaPromiseLoaded);
loader.loadBytes(imageBytes);
}
private function onMediaPromiseLoaded(e:Event):void
{
trace("Media file loaded");
var base64Enc:Base64Encoder = new Base64Encoder();
var mpLoaderInfo:LoaderInfo = e.target as LoaderInfo;
mpLoaderInfo.removeEventListener(Event.COMPLETE, onMediaPromiseLoaded);
var scale:Number = 0.25;
var matrix:Matrix = new Matrix();
matrix.scale(scale, scale);
var jpgQuality:int = 80;
var bmd:BitmapData = new BitmapData(mpLoaderInfo.width*scale, mpLoaderInfo.height*scale);
bmd.draw(mpLoaderInfo.content,matrix,null,null,null,true);
var rotatedBMD:BitmapData = rotateBitmapData(bmd, imageOrientation);
var bytes:ByteArray = rotatedBMD.encode(new Rectangle(0,0, rotatedBMD.width , rotatedBMD.height), new JPEGEncoderOptions(jpgQuality), bytes);
saveImageFile(bytes); //this is the smaller file saved. it does not have EXIF data but you can write your own using AS3 eXIF class.
}
private function rotateBitmapData( bitmapData:BitmapData, degree:int = 0 ) :BitmapData
{
var newBitmap:BitmapData;
var matrix:Matrix = new Matrix();
matrix.rotate( degree * (Math.PI / 180) );
if ( degree == 90 ) {
newBitmap = new BitmapData( bitmapData.height, bitmapData.width, true );
matrix.translate( bitmapData.height, 0 );
} else if ( degree == -90 || degree == 270) {
newBitmap = new BitmapData( bitmapData.height, bitmapData.width, true );
matrix.translate( 0, bitmapData.width );
} else if ( degree == 180 ) {
newBitmap = new BitmapData( bitmapData.width, bitmapData.height, true );
matrix.translate( bitmapData.width, bitmapData.height );
}else if(degree == 0){
newBitmap = new BitmapData( bitmapData.width, bitmapData.height, true );
//matrix.translate( bitmapData.width, bitmapData.height );
}
newBitmap.draw( bitmapData, matrix, null, null, null, true )
return newBitmap;
}
private function saveImageFile(ba:ByteArray):void{
var now:Date = new Date();
var filename:String = "IMG" + now.fullYear + now.month + now.day + now.hours + now.minutes + now.seconds + ".jpg";
var temp:File = File.documentsDirectory.resolvePath( filename );
var stream:FileStream = new FileStream();
stream.open( temp, FileMode.WRITE );
stream.writeBytes( ba );
stream.close();
}
private function getOrientation(jpeg:ByteArray):int{
if (jpeg == null) {
return 0;
}
var offset:int = 0;
var length:int = 0;
while (offset + 3 < jpeg.length && (jpeg[offset++] & 0xFF) == 0xFF) {
var marker:int = jpeg[offset] & 0xFF;
// Check if the marker is a padding.
if (marker == 0xFF) {
continue;
}
offset++;
// Check if the marker is SOI or TEM.
if (marker == 0xD8 || marker == 0x01) {
continue;
}
// Check if the marker is EOI or SOS.
if (marker == 0xD9 || marker == 0xDA) {
break;
}
// Get the length and check if it is reasonable.
length = pack(jpeg, offset, 2, false);
if (length < 2 || offset + length > jpeg.length) {
trace("Invalid length");
return 0;
}
// Break if the marker is EXIF in APP1.
if (marker == 0xE1 && length >= 8 &&
pack(jpeg, offset + 2, 4, false) == 0x45786966 &&
pack(jpeg, offset + 6, 2, false) == 0) {
offset += 8;
length -= 8;
break;
}
// Skip other markers.
offset += length;
length = 0;
}
if (length > 8) {
// Identify the byte order.
var tag:int = pack(jpeg, offset, 4, false);
if (tag != 0x49492A00 && tag != 0x4D4D002A) {
trace("Invalid byte order");
return 0;
}
var littleEndian:Boolean = (tag == 0x49492A00);
// Get the offset and check if it is reasonable.
var count:int = pack(jpeg, offset + 4, 4, littleEndian) + 2;
if (count < 10 || count > length) {
trace( "Invalid offset");
return 0;
}
offset += count;
length -= count;
// Get the count and go through all the elements.
count = pack(jpeg, offset - 2, 2, littleEndian);
while (count-- > 0 && length >= 12) {
// Get the tag and check if it is orientation.
tag = pack(jpeg, offset, 2, littleEndian);
if (tag == 0x0112) {
// We do not really care about type and count, do we?
var orientation:int = pack(jpeg, offset + 8, 2, littleEndian);
switch (orientation) {
case 1:
return 0;
case 3:
return 180;
case 6:
return 90;
case 8:
return 270;
}
trace( "Unsupported orientation");
return 0;
}
offset += 12;
length -= 12;
}
}
trace( "Orientation not found");
return 0;
}
private function pack(bytes:ByteArray,offset:int,length:int,
littleEndian:Boolean):int {
var step:int = 1;
if (littleEndian) {
offset += length - 1;
step = -1;
}
var value:int = 0;
while (length-- > 0) {
value = (value << 8) | (bytes[offset] & 0xFF);
offset += step;
}
return value;
}
}
}
You can use Stage.deviceOrientation or Stage.orientation* to determine which way round the phone is.
*not sure if this one works on iOS
Is it the BitmapData result itself that you want to rotate (ie create a new BitmapData with rotated image) or just rotate a Bitmap on the display list?
Edit:
Ok, heres some code to rotate a BitmapData object:
function rotateBitmapData(angle:int, source:BitmapData):BitmapData
{
var newWidth:int = source.rect.width;
var newHeight:int = source.rect.height;
if (angle==90 || angle==270)
{
newWidth = source.rect.height;
newHeight = source.rect.width;
}
var newBmd:BitmapData = new BitmapData(newWidth, newHeight, source.transparent);
var tx:Number = 0;
var ty:Number = 0;
if (angle==90 || angle==180)
{
tx = newWidth;
}
if (angle==180 || angle==270)
{
ty = newHeight;
}
var matrix:Matrix = new Matrix();
matrix.createBox(1, 1, Math.PI*angle/180, tx, ty);
newBmd.draw(source, matrix);
return newBmd;
}
angle should be 0,90,180 or 270. It will return a new BitmapData object rotated by specified angle.
You can use StageOrientationEvent.ORIENTATION_CHANGING Event :
stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGING, OrientationChangeHandler);
private function OrientationChangeHandler(e:StageOrientationEvent):void
{
switch (e.afterOrientation)
{
case StageOrientation.DEFAULT :
break;
case StageOrientation.ROTATED_RIGHT :
break;
case StageOrientation.ROTATED_LEFT :
break;
case StageOrientation.UPSIDE_DOWN :
break;
}
}
this can help you.