save and retrieval picture in sqlite with Qt and Java - android

i want to save picture in sqlite DB and then open that in java android!
in order to save picture i write a C++ program with Qt.
in C++ code i open picture as file, then convert it to QByteArray.
QFile file(foodImagePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
ShowError("Bad Image File.");
return;
}
QByteArray imageData = file.readAll();
but when i retrieve picture with sqliteManager the output is a bad file.(not correct picture )
then i decide to save picture as a Text in sqlite and convert it to base64 in C++ side!
QString(imageData.toBase64())
and in android side retrieve whit this code.
String imgString = cur.getString(0);
if( imgString != null)
{
byte[] imgByte = Base64.decode(imgString, Base64.DEFAULT);
pic = BitmapFactory.decodeByteArray(imgByte,0,imgByte.length);
}
and now the i can see the picture , but it was a little damaged!
can any body help me? PLZ.

An image file is not a text file.
Drop the QIODevice::Text flag.
(Using Base64 is not necessary if you handle the blobs correctly.)

Related

storing reference to image on SD efficiently in DB

I'm pretty new to programming Android and I have a problem - I'm creating an app which stores "funny" images and enables user to view all of them, rate them (at least I want to make it so ;) ). I was trying to make a very simple database which stores images as BLOBs but someone on forum told me that this is a bad idea - so I decided to store images on SD like (this code is mainly from Stack although):
private void savePicToSD(){
Toast.makeText(getActivity(),"Saving...",Toast.LENGTH_SHORT).show();
//getting BitMap from ImageView
Bitmap imageToSave=((BitmapDrawable)preview.getDrawable()).getBitmap();
// getting env var representing path
String root= Environment.getExternalStorageDirectory().toString();
Toast.makeText(getActivity(),root,Toast.LENGTH_LONG).show();
File dir=new File(root+DIR_NAME_);
dir.mkdirs();
String fileNam="Image-"+FILE_COUNTER_+".jpg";
FILE_COUNTER_=FILE_COUNTER_.add(BigInteger.valueOf(1));
File fileSav=new File(dir,fileNam);
//I don't use try-with-resources because of API lvl
FileOutputStream out=null;
try{
out=new FileOutputStream(fileSav);
//saving compressed file ot the dir
imageToSave.compress(Bitmap.CompressFormat.JPEG,90,out);
out.flush();
out.close();
}catch(Exception ex) {
ex.printStackTrace();
Toast.makeText(getActivity(), "Error during saving.", Toast.LENGTH_SHORT).show();
}
Toast.makeText(getActivity(),"Image saved.",Toast.LENGTH_LONG).show();
}
after making it I now don't know how to store this "reference" and how to implement "fetching data from DB" - I was thinking about storing (with some sort of other data releated to specific photo) in DB string contatining path to the image and later while reading data from DB, read also the image - but how to do this efficiently? (I know that fetching data from DB has to be done as a Thread (AsyncTask ? ))?
I think the best you can do is storing the path of the image in BDD or a relative path and then access it
Store imagePath/fileName in your local database along with saving the image in the file system(SD card), and then when you want to get back that image just fetch that image from the file system by using imagePath/fileName stored in database.
I hope that you know how to create file and store in SD card.

How to get the result of Runtime.getRuntime().exec directly

I'm working on a rooted android device. I'm trying to capture the screen and store the result in Bitmap for later usage.
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
path += "/img.png";
Process sh = Runtime.getRuntime().exec("su", null,null);
OutputStream os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + path).getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor();
final Bitmap x = BitmapFactory.decodeFile(path);
What I'm doing here is naming a path for a new image and capturing the screen using the command /system/bin/screencap -p FILEPATH. Then I read the image I stored in that file and use it in the bitmap.
My problem with my current code is that it's slow(not suitable for a real-time application). I'm now trying to make it faster. Instead of saving the captured picture into file and then reading it again from the program, I want to read it directly from the result of Runtime.getRuntime().exec(...)
In the description of the command screencap, I found that I can use it without specifying the output file name, and in this case the results will be printed to stdout.
I tried several codes to read the result byte array to use it directly in my code
final Bitmap x = BitmapFactory.decodeByteArray(resultArrayByte, 0, resultArrayByte.length);
but none of the codes worked with me.
How can I use sh's input/output streams to get the result byte array directly without saving the output into a file then loading it again?
Take a Look here, in this link you can find a library called ASL
a lot of questions in this post, i'm confused :)
i hope this link is useful for your requirements.

Unable to find saved image

I'm very new to Android. I'm working with Xamarin and I have to take a picture with camera and save the picture.
I achieved to take the picture, I have a Bitmap object. Then I save it without error but when I try to find it, there is no file.
There is my code :
Bitmap imgBmp = /* image initialized */
//Save image on folder
var folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var filePath = System.IO.Path.Combine(folderPath, "image1.png");
var stream = new System.IO.FileStream(filePath, FileMode.Create);
bool isOK = imgBmp.Compress(Bitmap.CompressFormat.Jpeg, 95, stream);
stream.Flush();
stream.Close();
I have no error when execute, isOK is true but when I search for the image.png I'm unable to find the file.
With debugger I saw that the path is : /data/user/0/com.myCompagny.MyAppli/files/image1.png but I can not see that folder.
Can someone help me to find my image1.png ?
Or to change the default folder to something like Pictures\MyApplication\image.png but I don't know how to find default folder for images.
Your file is there, but you have no permissions to see it. App directories are only readable to the owning app's uid. If you try to find your file through the shell, your uid is different.
You should try to use another folder path if you want to save file in a world readable location.
I'm just guessing but maybe System.Environment.SpecialFolder.CommonPictures would do.

Picture VS Byte Array?

I have pictures to send to a server from an android device.
I'm trying to find out which is perhaps better as a storage option.
I'm struggling to see which format will use less storage space on the device and server. I am already sending data(text) from Db's over to server.
So wondering if it's best to put the pictures as byte arrays into db (stored and sent) seeing as I have Db's already
OR
Keep the pic's out of db and send separate.
i hope this helps you
public byte[] extractBytes (String ImageName) throws IOException {
File imgPath = new File(ImageName);
BufferedImage bufferedImage = ImageIO.read(imgPath);
WritableRaster raster = bufferedImage .getRaster();
DataBufferByte data = (DataBufferByte) raster.getDataBuffer();
return ( data.getData() );
}

Convert Android image into a file that MATLAB can read

I´m working on a project related with image recognition using MATLAB and I'm currently using an Android app to help with some pre-processing steps. I thought it was going to be easy to work with matrices instead of bitmaps. I finally managed to finish my algorithm and to import it to Eclipse. The problem is that I realize that I don't know how to convert a Bitmap image into something that MATLAB can read in for the purposes of my algorithm.
Do you have any ideas on how I can do this?
If I'm interpreting your question correctly, you have an image stored in the Bitmap class and you want to save this to file locally on your Android device. You then want to load this image into MATLAB for your image recognition algorithm.
Given the fact that your image is in memory via Android, you can use the method compress: http://developer.android.com/reference/android/graphics/Bitmap.html#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream
You'd then use this and save the image to file, and then you can load it into MATLAB, using imread for example.
Here's some sample code you could write for your Android app. Assuming your Bitmap instance is stored in a variable called bmp, do:
FileOutputStream out = null; // For writing to the device
String filename = "out.png"; // Output file name
// Full path to save
// This accesses the pictures directory of your device and saves the file there
String output = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), filename);
try {
out = new FileOutputStream(filename); // Open up a new file stream
// Save the Bitmap instance to file
// First param - type of image
// Second param - Compression factor
// Third param - The full path to the file
// Note: PNG is lossless, so the compression factor (100) is ignored
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
}
// Catch any exceptions that happen
catch (Exception e) {
e.printStackTrace();
}
// Execute this code even if exception happens
finally {
try {
// Close the file if it was open to write
if (out != null)
out.close();
}
// Catch any exceptions with the closing here
catch (IOException e) {
e.printStackTrace();
}
}
The above code will save the image to your default Pictures directory on your device. Once you pull out that image, you can read the image into MATLAB by using imread:
im = imread('out.png');
im would thus be the raw RGB pixels of the image that you can now use for your image recognition algorithm.

Categories

Resources