Camera SetPreviewCallback best way to convert YUV to RGB - android

I am using this code to write an image to SD after camera.takePicture is called:
protected String doInBackground(byte[]... jpeg) {
File directory=new File(Environment.getExternalStorageDirectory() + "/" + getString(R.string.qpw_picture_path) + "/" + getString(R.string.qpw_picture_title) + "_" + initialTime);
directory.mkdirs();
String currentTime = new SimpleDateFormat(getString(R.string.qpw_date_format)).format(new Date());
File photo = new File (directory, getString(R.string.qpw_picture_title) + "_" + currentTime + "_" + current + ".jpg");
current++;
if (photo.exists()) {
photo.delete();
}
try {
FileOutputStream fos=new FileOutputStream(photo.getPath());
fos.write(jpeg[0]);
fos.flush();
fos.close();
}
catch (java.io.IOException e) {
}
new ImageMediaScanner(getBaseContext(), photo);
return(null);
}
Which is working fine in this case, but when I am using the same code to write images from camera.setPreviewCallback, I end up with 450KB corrupted images on SD which cannot be used or even opened.
Any help or advice would be appreciated.
Edit:
It seems that data should be first converted from YUV to RGB before saving. Having tried one of the many code samples found on Google and SO, I was facing no more issues.
Does anyone know what is the best way of doing it? In terms of speed, memory allocation, CPU...

Just in case someone stumbles on the same issue this SO post summarize well what I was looking for.

Related

Android BitmapFactory decodeResource Out of Memory Exception

I'm new to android and developing an app that saves large images from drawable folder to phone storage. These files have resolution of 2560x2560 and I want to save these files without loosing image quality.
I use following method to save images and it gives me Out of Memory Exception. I have seen many answers how to load a large bitmap efficiently. But I cant really find an answer for this problem.
In my code, I use
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imageId);
File file = new File(root.getAbsolutePath() + "/Pictures/" + getResources().getString(R.string.app_name) + "/" + timeStamp + ".jpg");
file.createNewFile();
FileOutputStream oStream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, oStream);
oStream.close();
bitmap.recycle();
Is there anything wrong with my code? This works without any exception for smaller images.
If I use android:largeHeap="true", this does not throw any exception. But I know it is not a good practice to use android:largeHeap="true".
Is there any efficient way to save large images from drawable folder without an exception?
Thank you in advance.
If you just want to copy the image file, you shouldn't decode it into a bitmap in the first place.
You can copy a raw resource file with this for example:
InputStream in = getResources().openRawResource(imageId);
String path = root.getAbsolutePath() + "/Pictures/" + getResources().getString(R.string.app_name) + "/" + timeStamp + ".jpg";
FileOutputStream out = new FileOutputStream(path);
try {
byte[] b = new byte[4096];
int len = 0;
while ((len = in.read(b)) > 0) {
out.write(b, 0, len);
}
}
finally {
in.close();
out.close();
}
Note that you have to store your image in the res/raw/ directory instead of res/drawable/.

saving bitmap from arraylist to SD card - saved file unreadable

My situation is as follows: I'm saving multiple bitmaps from an arraylist to a specific folder in my devices SD Card (with success), however, the saved file- when clicked- prompts a message from the phone, stating: "Unable to find application to perform this action." The file size of this file is proportional to that of the bitmap image being saved, so I'm a bit confused, as the device has no problems opening image files, yet cannot open (or identifiy) these as a media file.
Question: What would cause the saved image file (presuming that I have saved it correctly) to exhibit this type of behavior in a device, and how should I resolve this issue?
Extra: the thumbnail of the file is the system provided thumbnail of the two papers on top of each other. The arraylist is being passed from one activity to its current one where the method provided is supplied.
Here is the method invoking the saving of the files to the specified folder /filesdestination:
private void saveImages(){
// to retrieve bitmaps
ArrayList<Bitmap> images = getIntent().getParcelableArrayListExtra("images key");
//to retrieve bitmaps and save in specific order, while also naming them in that order
int loopVal = 0;
int postVal = 9;
while ( loopVal < 9) {
Bitmap Image = images.get(loopVal);
try {
String filedestination = new String(Environment.getExternalStorageDirectory() + "/filedestination");
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
File file = new File(filedestination, postVal + ".post_order" + ".jpg" + timeStamp);
File picfile = file;
FileOutputStream fos = new FileOutputStream(picfile);
Image.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Throwable e) {
e.printStackTrace();
}
postVal--;
loopVal++;
}
}
Any insight would be appreciated,
-Lucas
i think it cannot read the file type because the timestamp is after the file extension jpg and you are also compressing it as a png, so you might want to change either or, something like this
File file = new File(filedestination, postVal + timeStamp +".post_order" + ".png");
It seems that you are saving a .jpg file compressed as a PNG. That can make the image reader app to misbehave.
Either change Image.compress(Bitmap.CompressFormat.PNG, 100, fos);
to
Image.compress(Bitmap.CompressFormat.JPEG, 100, fos);
or change
File file = new File(filedestination, postVal + ".post_order" + ".jpg" + timeStamp);
to
File file = new File(filedestination, postVal + ".post_order" + ".png" + timeStamp);

How to capture to system default album using Camera class

I just complete my photo app to using "Camera" class related implement.
However I could not see my capture photo in default Android photo album app.
Here is my code segment from my callback from Camera.takePicture:
PictureCallback jpegPictureCallback = new PictureCallback() {
#Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
Bitmap bmp = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
String fileName = Environment.getExternalStorageDirectory()
.toString()
+ File.separator
+ "DCIM"
+ File.separator
+ "Camera"
+ File.separator
+ "PicTest_" + System.currentTimeMillis() + ".jpg";
File file = new File(fileName);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdir();
}
try {
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(file));
bmp.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bos.flush();
bos.close();
Toast.makeText(MainPage.this, "Picture work " + fileName + "!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
// TODO Auto-generated catch block
// e.printStackTrace();
Toast.makeText(MainPage.this, "Picture Failed" + e.toString(),
Toast.LENGTH_LONG).show();
}
};
};
Please provide any idea for this, thank you.
Use MediaScannerConnection to tell the MediaStore to index your photo.
Also, please use getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), rather than the hardcoded subdirectory that you are using.
Also also, rather than decoding the byte[] to a Bitmap, then re-encoding it as a byte[], you might consider just saving the byte[].
Also also also, onPictureTaken() is called on the main application thread AFAIK; please do your image manipulations and saving to disk in a background thread.

Android SQLite read blob

I have an sqlite database which is written to from a service running on windows(C++). I am now trying to read from this same sqlite database which contains some blob data. I have some code as follows:
String tileQuery = "SELECT * FROM '" + layerName + "' WHERE zoom_level=?";
Cursor tileCursor = database.rawQuery(tileQuery, new String[] {zoom_level});
if( tileCursor.moveToFirst() )
{
while( !tileCursor.isAfterLast() )
{
int tileRow = tileCursor.getInt(tileCursor.getColumnIndex("tile_row"));
int tileColumn = tileCursor.getInt(tileCursor.getColumnIndex("tile_column"));
byte[] tileData = tileCursor.getBlob(tileCursor.getColumnIndex("tile_data"));
//Write tile to file
String fileName = layerName + "_" + zoom_level + "_" + tileRow + "_" + tileColumn + ".jpeg";
try {
/*
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + TILE_STORAGE_PATH + "/" + fileName));
bos.write(tileData);
bos.flush();
bos.close();
*/
ByteBuffer bb = ByteBuffer.wrap(tileData);
bb.order(ByteOrder.LITTLE_ENDIAN);
FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + TILE_STORAGE_PATH + "/" + fileName);
byte[] toWrite = new byte[bb.remaining()];
bb.get(toWrite, 0 , toWrite.length);
fos.write(toWrite);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
tileCursor.moveToNext();
}
}
As shown, I am attempting to write the blobs to disk as jpeg images. No matter what I do, the images appear to be corrupt, as in I cannot view them on any image viewer within android. The same images can be written to file on windows and viewed correctly, which made me think that it was an endianess issue(due to the fact that the blob was written to the database via a service running on windows). I have tried changing the byte order and writing to disk again, but I get the same result. Could anyone suggest what I might be doing wrong/missing. Any help is greatly appreciated.
To make this work there are a few different steps. Assuming your database connection is working and those are the correct columns you are looking in with your Cursor
(1) Convert the blob to a Bitmap. You can use the blob you get back, assuming you actually downloaded and stored it to your local database, as the byte[] you will decode.
Bitmap bm = BitmapFactory.decodeByteArray(tileData, 0 ,tileData.length);
(2) Create a new file in the approprite directory and write to that file. You can do that with something like the code below. The idea is to get the local directory
private void storeBitmap(Bitmap myBitmap){
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/your_directory_name");
String fname = "your_file_name.jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
If you want to add the images to gallery or you just want a different (and potentially easier) way to add the file, look into using MediaScanner which will add the files as though you took the picture with your camers

Android sdk, Apart from saving image in sd card, are there any places i can save the photo to?

I have read alot of references from the web about android storage for images. Most of them were saying Images are stored insided sd card same as photos that was taken with camera.
Does android allow us to store them in local memory in the android phone rather than sd cards?
To open/create a file in the internal storage, use openFileOutput(String name, int mode)
http://developer.android.com/reference/android/content/Context.html#openFileOutput(java.lang.String, int)
You don't want to save pictures to your local memory. Some users only have a few MB of free application space - which would be killed by 1-2 pictures. I know my old G1 wouldn't accept call, texts, or any notifications when it was out of space - it becomes a paperweight. Maybe later versions are better.
The answer though, is yes. You can save the image to sqlite database which is local - not on SD card.
I use getCacheDir() as described in HERE under Saving cache files.
yeah you can store the images in cache storage , it will be private no one can see the image except from your application package.
public static String saveToCacheMemory(Activity activity, Bitmap
bitmapImage){
SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyyMMddHHmmss", Locale.US);
ContextWrapper cw = new ContextWrapper(activity);
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,mDateFormat.format(new Date()) + StaticVariables.imageFormat);
Log.d(TAG, "directory: " + directory);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
Log.d(TAG, "bit exception: Success" );
} catch (Exception e) {
Log.d(TAG, "bit exception: " + e.getMessage());
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
Log.d(TAG, "io exce: " + e.getMessage());
}
}
Log.d(TAG, "absolute path " + directory.getAbsolutePath());
return mypath.getAbsolutePath();
}
if you have more questions you can check my blog post

Categories

Resources