How to convert to List<int> to File? - dart

I need to store image file to SQLite database using SQLite.
I used this method,
var image = await ImagePicker.pickImage(source: imageSource);
List<int> bytes = await image.readAsBytes();
https://stackoverflow.com/a/55258035/11065582
QUESTION 1
- what is the type when create table?(CREATE TABLE registerTable(image ?,)
QUESTION 2
- how to convert to again File?

If you have a List<int> that represents a list of 8-bit bytes and want to write it to a file, you can simply use File.writeAsBytes. (It's the inverse of File.readAsBytes, which you're already using.)
await File(desiredDestinationPath).writeAsBytes(bytes);
All that said, why do you need to write to a File? If you already have the image data and want to show it, you can create a MemoryImage (or use Image.memory if you need an Image widget).

Related

Dart Image to File in local storage

I'm using the Image class from the package image. I have to download a image from internet and save it in my local storage. Then modify this image and save the changes. I have done the first 2 steps, but when i'm saving the file, this is corrupted and the Image Viewer dont recognizit as a image file. This is the code where the image is saved after the changes.
var response = await client.get(urlToImage);
var img = await File('./network_image.jpg').writeAsBytes(response.bodyBytes);
Image image = decodeImage(img.readAsBytesSync())
var f = await File('./image.jpg').writeAsBytes(image.getBytes()); // this doesnt work
// var f = await File('./image.jpg').writeAsBytes(image.data); // this doesnt work
The File.writeAsBytes expects a List<int> bytes, and the Image.data and Image.getBytes() returns Uint32Listand Uint8List respectively.
I'm not using Flutter, only dart for a command line program.
The image object stores the image data in an unencoded format. Remember you decoded it with decodeImage. It stores the raw colors of the image. You need to re encode the image data or your image viewer needs to handle whatever image format of data getBytes returns.
The easier solution is probably to re encode. You seem to want a JpegEncoder.
var f = await File('./image.jpg').writeAsBytes(JpegEncoder().encodeImage(image));

How to select the type of file when using SaveImage()

I have realize that the SaveImage() command uses the last the last type of format that has been selected during normal DM operation. I assume that this option is selected somewhere in the GobalInfo tags. Please, could someone tell me which tag I have to modify to select dm4 format when I use SaveImage()?
'SaveImage()' is just a convenience wrapper. It is generally not the
Image which is saved to file, but an ImageDocument which can contain one ore more images. The latest DigitalMicograph help
documentation is more detailed about loading/saving than previous
ones, so I'm just copy-pasting the according passages below:
For example to store the front-most displayed image(document) as DM images, you may use:
string name = "C:\\TempImg"
string handler = "Gatan 3 Format"
ImageDocument doc = GetFrontImageDocument()
doc.ImageDocumentSaveToFile( handler, name )
And you can always get the ImageDocument from any image, using:
string handler = "Gatan 3 Format"
image img := RealImage("Test - not yet shown", 4, 100, 100 )
string name = "C:\\" + img.GetName()
ImageDocument doc = img.ImageGetOrCreateImageDocument()
doc.ImageDocumentSaveToFile( handler, name )

How to read Microsoft Word Binary Data from database and convert it to readable text

I'm working in a java application called Mirth where I need to read a saved word document that is saved in a database table in a Microsoft word binary data format. Currently I can retrieve data from column in my java application but I need to convert this to readable text or XML or HTML format.
Looking online there is a java library call Aspose.words but I can't find any methods that would read in this binary data and convert it to something readable. Has anyone use Aspose.words before to do a task like this or does anyone have an alternative solution
Load document from Database
You can load the Word document using ByteArrayInputStream, if it's in a database table. Please refer to http://www.aspose.com/docs/display/wordsjava/How+to++Load+and+Save+a+Document+to+Database for an article that explains saving and reading a Word document to/from database. I have copied the relevant code from there.
public static Document readFromDatabase(String fileName) throws Exception
{
// Create the SQL command.
String commandString = "SELECT * FROM Documents WHERE FileName='" + fileName + "'";
// Retrieve the results from the database.
ResultSet resultSet = executeQuery(commandString);
// Check there was a matching record found from the database and throw an exception if no record was found.
if(!resultSet.isBeforeFirst())
throw new IllegalArgumentException(MessageFormat.format("Could not find any record matching the document \"{0}\" in the database.", fileName));
// Move to the first record.
resultSet.next();
// The document is stored in byte form in the FileContent column.
// Retrieve these bytes of the first matching record to a new buffer.
byte[] buffer = resultSet.getBytes("FileContent");
// Wrap the bytes from the buffer into a new ByteArrayInputStream object.
ByteArrayInputStream newStream = new ByteArrayInputStream(buffer);
// Read the document from the input stream.
Document doc = new Document(newStream);
// Return the retrieved document.
return doc;
}
Read text
Once the file is loaded, you can read it's paragraphs, tables, images etc using DOM, see the related documentation at http://www.aspose.com/docs/display/wordsjava/Programming+with+Documents.
But, if you just want to get all the text from a document, you can do it easily by calling toString() method as below
System.out.println(doc.toString(SaveFormat.TEXT));
I work with Aspose as Developer Evangelist.

MvvmCross ImageView Binding to Byte Array

I am currently trying to bind a Android ImageView to a Byte array that I have setup as my ViewModel.
private Byte[] _currentActivityImage;
public Byte[] CurrentActivityImage
{
get { return _currentActivityImage; }
set { _currentActivityImage = value; RaisePropertyChanged(() => CurrentActivityImage); }
}
I am doing it this way as I am downloading the image as part of the large data download and storing that image data into the database so it's tired directly to a specific record in the table.
I haven't been able to find any information on specifically how to bind the Android ImageView (Or the MvxImageView) to a Byte array or a raw bitmap image.
I saw in one of the posts that Stuart mentioned that capability exists (or maybe I read it wrong.), but was not able to find the correct bindings to use.
(Note: if need be, I can change the property to use Bitmap, but figured that the Byte array might be more cross-platform compatible.)
The picture choose sample shows one way to do this - https://github.com/MvvmCross/MvvmCross-Tutorials/tree/master/PictureTaking
The N+1 N=12,etc CollectABull sample shows another - going via a file https://github.com/MvvmCross/NPlus1DaysOfMvvmCross/tree/master/N-12-CollectABull/

Sending javaMail attachement of any type from database

I'm have domain class with property that represents files uploaded on my GSP. I've defined that file as byte array (byte [] file). When some specific action happens I'm sending mail with attachments from. This is part of my SendMail service:
int i = 1;
[requestInstance.picture1, requestInstance.picture2, requestInstance.picture3].each(){
if(it.length != 0){
DataSource image = new ByteArrayDataSource(it, "image/jpeg");
helper.addAttachment("image" + i + ".jpg", image);
i++;
}
}
This works fine with image files. But now I want to be able to work with all file types and I'm wondering how to implement this. Also, I want to save real file name in database. All help is welcomed.
You can see where the file name and MIME type are specified in your code. It should be straightforward to save and restore that information from your database along with the attachment data.
If you're trying to figure out from the byte array of data what the MIME type is and what a good filename would be, that's a harder problem. Try this.

Resources