Call Bitmap inside onPictureTaken - android

How can I call Bitmap Inside a onPictureTaken method ?. Image is not storing in a gallery until and unless I restart my phone
Please Help in re-arranging the code.
public static int getOrientation(Context context,Uri photoUri){
Cursor cursor = context.getContentResolver().query(photoUri,new String[] {MediaStore.Images.ImageColumns.ORIENTATION},null,null,null);
if(cursor.getCount()!=1){
return -1;
}
cursor.moveToFirst();
return cursor.getInt(0);
}
getCorrectlyOrientatedImage() is never used. Please help me in where can I call this function.
public static Bitmap getCorrectlyOrientatedImage(Context context, Uri photoUri)throws IOException{
InputStream is=context.getContentResolver().openInputStream(photoUri);
BitmapFactory.Options dbo = new BitmapFactory.Options();
dbo.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is,null,dbo);
is.close();
int rotatedWidth,rotatedHeight;
int orientation = getOrientation(context,photoUri);
if(orientation == 90 || orientation ==270){
rotatedWidth=dbo.outHeight;
rotatedHeight=dbo.outWidth;
}else{
rotatedWidth=dbo.outWidth;
rotatedHeight=dbo.outHeight;
}
Bitmap srcBitmap;
is = context.getContentResolver().openInputStream(photoUri);
if(rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION ){
float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION);
float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION);
float maxRatio = Math.max(widthRatio,heightRatio);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = (int) maxRatio;
srcBitmap = BitmapFactory.decodeStream(is, null, options);
}else{
srcBitmap = BitmapFactory.decodeStream(is);
}
is.close();
if(orientation > 0){
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
srcBitmap = Bitmap.createBitmap(srcBitmap,0,0,srcBitmap.getWidth(),srcBitmap.getHeight(),matrix,true);
}
return srcBitmap;
}
private PictureCallback mPicture = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
Log.d("onPictureTaken", "File is assigned to pictureFile");
if (pictureFile == null) {
Log.d(DEBUG_TAG, " Error creating a media file, check storage permissions:");
return;
}
try {
Bitmap bitmap=getCorrectlyOrientatedImage()
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "SELFie");
String path = mediaStorageDir.getPath() + File.separator + "IMG_Some_name.jpg";
SCamera.this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));
} catch (FileNotFoundException e) {
Log.d("DG_DEBUG", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("DG_DEBUG", "Error accessing file: " + e.getMessage());
}
}
};
Image is not storing in a SDcard Untill I reboot my phone and also image which is stored is stored in a wrong direction. If photo is taken in portrait , image is stored in landscape . How can store the image in correct orientation.
private static File getOutputMediaFile(int type){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"SELFie");
if(!mediaStorageDir.exists()){
if(!mediaStorageDir.mkdir()){
Log.d(DEBUG_TAG,"Filed to create directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}

I know this question is a little old, but just in case someone faces the same issue, the solution is to save the image in a background thread as shown in this github link.
private PictureCallback mPicture = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
new SaveImageTask().execute(data);
}
};
private class SaveImageTask extends AsyncTask<byte[], Void, Void> {
#Override
protected Void doInBackground(byte[]... data) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
Log.d("onPictureTaken", "File is assigned to pictureFile");
if (pictureFile == null) {
Log.d(DEBUG_TAG, " Error creating a media file, check storage permissions:");
return null;
}
try {
Bitmap bitmap=getCorrectlyOrientatedImage()
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "SELFie");
String path = mediaStorageDir.getPath() + File.separator + "IMG_Some_name.jpg";
SCamera.this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));
} catch (FileNotFoundException e) {
Log.d("DG_DEBUG", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("DG_DEBUG", "Error accessing file: " + e.getMessage());
}
}
}

Related

Android Camera and shader effects

I am working an android camera app that uses a vertex shader to apply various distortion effects to the camera preview. Although the camera preview shows the desired effect, pictures do not. How do I get the picture/image to show the same effect as the camera preview? I hope my question is clear. Many thanks in anticipation.
Code extract is as follows:
Camera Activity:
public class CameraActivity extends Activity implements SurfaceTexture.OnFrameAvailableListener{
#SuppressWarnings("deprecation")
private Camera mCamera;
private PreviewCallback mPreviewCallback;
private MyGLSurfaceView glSurfaceView;
private SurfaceTexture surface;
MyGL20Renderer renderer;
public boolean mFrameAvailable = false;
public static final int MEDIA_TYPE_IMAGE = 1;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.screen);
FrameLayout surface = (FrameLayout)findViewById(R.id.camera_preview);
glSurfaceView = new MyGLSurfaceView(this);
renderer = glSurfaceView.getRenderer();
// setContentView(glSurfaceView);
surface.addView(glSurfaceView);
Button captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
#SuppressWarnings("deprecation")
#Override
public void onClick(View v) {
// get an image from the camera
// mCamera.takePicture(null, null, mPicture);
// mCamera.setOneShotPreviewCallback(mPreviewCallback);
takeSnapPhoto();
}
}
);
}
#SuppressWarnings("deprecation")
public void startCamera(int texture)
{
surface = new SurfaceTexture(texture);
surface.setOnFrameAvailableListener(this);
renderer.setSurface(surface);
mCamera = Camera.open(CameraInfo.CAMERA_FACING_BACK);
try
{
mCamera.setPreviewTexture(surface);
mCamera.startPreview();
}
catch (IOException ioe)
{
Log.w("MainActivity","CAM LAUNCH FAILED");
}
Camera.Parameters param = mCamera.getParameters();
param.setPictureSize(640, 480);
// param.setColorEffect(Camera.Parameters.EFFECT_SEPIA);
mCamera.setParameters(param);
}
public void onFrameAvailable(SurfaceTexture surfaceTexture)
{
mFrameAvailable = true;
glSurfaceView.requestRender();
}
#SuppressWarnings("deprecation")
#Override
public void onPause()
{
mFrameAvailable = false;
mCamera.stopPreview();
mCamera.release();
System.exit(0);
}
#SuppressWarnings("deprecation")
private PictureCallback mPicture = new PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
// Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data .length);
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null){
Log.d("CrazyMirror", "Error creating media file, check storage permissions: ");
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
// bitmap.compress(Bitmap.CompressFormat.PNG,100, fos);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d("CrazyMirror", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("CrazyMirror", "Error accessing file: " + e.getMessage());
}
refreshPreview();
}
};
/** Create a File for saving an image or video */
#SuppressLint("SimpleDateFormat")
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "CrazyMirror");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("CrazyMirror", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
}
else if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
}
else {
return null;
}
return mediaFile;
}
#SuppressWarnings("deprecation")
public void refreshPreview() {
try {
mCamera.stopPreview();
} catch (Exception e) {}
try {
mCamera.startPreview();
} catch (Exception e) {}
}
public void takeSnapPhoto() {
mCamera.setOneShotPreviewCallback(new Camera.PreviewCallback() {
#Override
public void onPreviewFrame(byte[] data, Camera camera) {
Camera.Parameters parameters = camera.getParameters();
int format = parameters.getPreviewFormat();
//YUV formats require more conversion
if (format == ImageFormat.NV21 || format == ImageFormat.YUY2 || format == ImageFormat.NV16) {
int w = parameters.getPreviewSize().width;
int h = parameters.getPreviewSize().height;
// Get the YuV image
YuvImage yuv_image = new YuvImage(data, format, w, h, null);
// Convert YuV to Jpeg
Rect rect = new Rect(0, 0, w, h);
ByteArrayOutputStream output_stream = new ByteArrayOutputStream();
yuv_image.compressToJpeg(rect, 100, output_stream);
byte[] byt = output_stream.toByteArray();
FileOutputStream outStream = null;
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "CrazyMirror");
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
try {
// Write to SD Card
// File file = createFileInSDCard(FOLDER_PATH, "Image_"+System.currentTimeMillis()+".jpg");
//Uri uriSavedImage = Uri.fromFile(file);
outStream = new FileOutputStream(mediaFile);
outStream.write(byt);
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
}
});}
}

takePicture() landscape mode rotation android

I am taking an image from camera using:
mCamera.takePicture(null, null,
new PhotoHandler(getApplicationContext()));
But the image I am taking is landscape and I want it portrait, I tried converting the file into bitmap, rotate it and then save it again, here is my onPictureTaken() function and rotate() function inside PhotoHandler.class:
#Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFileDir = getDir();
if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
Log.d("Error", "Can't create directory to save image.");
Toast.makeText(context, "Can't create directory to save image.",
Toast.LENGTH_LONG).show();
return;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "Picture_" + date + ".jpg";
String filename = pictureFileDir.getPath() + File.separator + photoFile;
pictureFile = new File(filename);
st1 = pictureFile.getAbsolutePath();
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
Toast.makeText(context, "New Image saved:" + photoFile,
Toast.LENGTH_LONG).show();
} catch (Exception error) {
Log.d("Error", "File" + filename + "not saved: "
+ error.getMessage());
Toast.makeText(context, "Image could not be saved.",
Toast.LENGTH_LONG).show();
}
Bitmap b = rotate(BitmapFactory.decodeFile(pictureFile.getAbsolutePath()),270);
FileOutputStream out = null;
try {
out = new FileOutputStream(pictureFile);
b.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
CameraPreview.safeToTakePicture = true;
galleryAddPic();
}
public static Bitmap rotate(Bitmap bitmap, int degree) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
mtx.setRotate(degree);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}
Anyway, it takes too long, I tried using mCamera.setDisplayOrientation(90); but it changes only the preview of the camera, the picture taken is still the same, I also tried getting the parameters of the camera and changing it like here:
Camera.Parameters parameters = mCamera.getParameters();
parameters.set("orientation", "portrait");
mCamera.setParameters(parameters);
Without success, any help will be appreciated.
EDIT :
New code:
#Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFileDir = getDir();
if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
Log.d("Error", "Can't create directory to save image.");
Toast.makeText(context, "Can't create directory to save image.",
Toast.LENGTH_LONG).show();
return;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "Picture_" + date + ".jpg";
String filename = pictureFileDir.getPath() + File.separator + photoFile;
pictureFile = new File(filename);
st1 = pictureFile.getAbsolutePath();
Bitmap b = rotate(BitmapFactory.decodeByteArray(data,0,data.length),270);
FileOutputStream out = null;
try {
out = new FileOutputStream(pictureFile);
b.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
CameraPreview.safeToTakePicture = true;
galleryAddPic();
}
Anyway, it takes too long
Your current algorithm looks like this:
Start with the photo in memory
Save the photo to disk, which is slow
Read the photo back in from disk to memory, which is slow
Rotate the reloaded photo
A more efficient approach would be:
Start with the photo in memory
Rotate the photo
BitmapFactory has decodeByteArray() to get a Bitmap back for rotation purposes.

How Capture Picture while mobile vision api - face tracking

I'm using the Mobile vision api's face tracking example and i'm trying to take picture with tapping on the screen. Firstly i wanted to take any picture on the screen with button and i tryed this code but it failed. I look at the barcode reader example and there is tap method but i couldn't succeed. What approach that i should use this case?
pure FaceTracking github code
private void takeImage() {
camera.takePicture(null, null, new PictureCallback() {
private File imageFile;
#Override
public void onPictureTaken(byte[] data, Camera camera) {
try {
// convert byte array into bitmap
Bitmap loadedImage = null;
Bitmap rotatedBitmap = null;
loadedImage = BitmapFactory.decodeByteArray(data, 0,
data.length);
// rotate Image
Matrix rotateMatrix = new Matrix();
rotateMatrix.postRotate(rotation);
rotatedBitmap = Bitmap.createBitmap(loadedImage, 0, 0,
loadedImage.getWidth(), loadedImage.getHeight(),
rotateMatrix, false);
String state = Environment.getExternalStorageState();
File folder = null;
if (state.contains(Environment.MEDIA_MOUNTED)) {
folder = new File(Environment
.getExternalStorageDirectory() + "/Demo");
} else {
folder = new File(Environment
.getExternalStorageDirectory() + "/Demo");
}
boolean success = true;
if (!folder.exists()) {
success = folder.mkdirs();
}
if (success) {
java.util.Date date = new java.util.Date();
imageFile = new File(folder.getAbsolutePath()
+ File.separator
+ new Timestamp(date.getTime()).toString()
+ "Image.jpg");
imageFile.createNewFile();
} else {
Toast.makeText(getBaseContext(), "Image Not saved",
Toast.LENGTH_SHORT).show();
return;
}
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
// save image into gallery
rotatedBitmap.compress(CompressFormat.JPEG, 100, ostream);
FileOutputStream fout = new FileOutputStream(imageFile);
fout.write(ostream.toByteArray());
fout.close();
ContentValues values = new ContentValues();
values.put(Images.Media.DATE_TAKEN,
System.currentTimeMillis());
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.MediaColumns.DATA,
imageFile.getAbsolutePath());
CameraDemoActivity.this.getContentResolver().insert(
Images.Media.EXTERNAL_CONTENT_URI, values);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
#Override
public void onClick(View v) {
case R.id.captureImage:
takeImage();
break;
default:
break;
}
}
I solved the problem.
findViewById(R.id.capture).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCameraSource.takePicture(null, new CameraSource.PictureCallback() {
private File imageFile;
#Override
public void onPictureTaken(byte[] bytes) {
try {
// convert byte array into bitmap
Bitmap loadedImage = null;
Bitmap rotatedBitmap = null;
loadedImage = BitmapFactory.decodeByteArray(bytes, 0,
bytes.length);
Matrix rotateMatrix = new Matrix();
rotateMatrix.postRotate(rotation);
rotatedBitmap = Bitmap.createBitmap(loadedImage, 0, 0,
loadedImage.getWidth(), loadedImage.getHeight(),
rotateMatrix, false);
dir = new File(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyPhotos");
boolean success = true;
if (!dir.exists())
{
success = dir.mkdirs();
}
if (success) {
java.util.Date date = new java.util.Date();
imageFile = new File(dir.getAbsolutePath()
+ File.separator
+ new Timestamp(date.getTime()).toString()
+ "Image.jpg");
imageFile.createNewFile();
} else {
Toast.makeText(getBaseContext(), "Image Not saved",
Toast.LENGTH_SHORT).show();
return;
}
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
// save image into gallery
rotatedBitmap.compress(CompressFormat.JPEG, 100, ostream);
FileOutputStream fout = new FileOutputStream(imageFile);
fout.write(ostream.toByteArray());
fout.close();
ContentValues values = new ContentValues();
values.put(Images.Media.DATE_TAKEN,
System.currentTimeMillis());
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.MediaColumns.DATA,
imageFile.getAbsolutePath());
FaceTrackerActivity.this.getContentResolver().insert(
Images.Media.EXTERNAL_CONTENT_URI, values);
//saveToInternalStorage(loadedImage);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
});
Less lines of code
#Override
public void onPictureTaken(byte[] bytes) {
Log.d(TAG, "onPictureTaken - jpeg");
capturePic(bytes);
}
private void capturePic(byte[] bytes) {
try {
String mainpath = getExternalStorageDirectory() + separator + "MaskIt" + separator + "images" + separator;
File basePath = new File(mainpath);
if (!basePath.exists())
Log.d("CAPTURE_BASE_PATH", basePath.mkdirs() ? "Success": "Failed");
File captureFile = new File(mainpath + "photo_" + getPhotoTime() + ".jpg");
if (!captureFile.exists())
Log.d("CAPTURE_FILE_PATH", captureFile.createNewFile() ? "Success": "Failed");
FileOutputStream stream = new FileOutputStream(captureFile);
stream.write(bytes);
stream.flush();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private String getPhotoTime(){
SimpleDateFormat sdf=new SimpleDateFormat("ddMMyy_hhmmss");
return sdf.format(new Date());
}

Save image to custom folder android on onPictureTaken

i need to save an image to a custom folder in my phone.
my code is
#Override
public void onPictureTaken(byte[] data, Camera camera) {
if (data != null) {
FileOutputStream outStream = null;
try {
Random generator = new Random();
int n = 0000;
n = generator.nextInt(n);
String fName = "Image" + n + ".jpg";
outStream = new FileOutputStream(String.format(fName));
outStream.write(data);
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
camera.startPreview();
}
not working
#Override
public void onPictureTaken(byte[] data, Camera camera) {
if (data != null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data .length);
if(bitmap!=null){
File file=new File(Environment.getExternalStorageDirectory()+"/dirr");
if(!file.isDirectory()){
file.mkdir();
}
file=new File(Environment.getExternalStorageDirectory()+"/dirr",System.currentTimeMillis()+".jpg");
try
{
FileOutputStream fileOutputStream=new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG,100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
}
catch(IOException e){
e.printStackTrace();
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}
}
Dont forget to add permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
You can find ans from below code
#Override
public void onPictureTaken(byte[] data, Camera camera) {
if (data != null) {
Bitmap m_bitmap = BitmapFactory.decodeByteArray(data , 0, data .length);
String destinationPath = Environment.getExternalStorageDirectory() + "/" + "Image.jpg";
if (m_bitmap != null || destinationPath != null)
{
FileOutputStream m_out = new FileOutputStream(destinationPath);
m_bitmap.compress(Bitmap.CompressFormat.JPEG, 100, m_out);
}
}
Hope this code helps you
You first have to create the destination folder you want to save the file into. So, in your Android sdk folder, you create the MediaFiles folder. In this folder you can then create your file.
Here's some code you can try.
#Override
public void onPictureTaken(byte[] data, Camera camera) {
if (data != null) {
FileOutputStream outStream = null;
try {
Random generator = new Random();
int n = 0000;
n = generator.nextInt(n);
String fName = "Image" + n + ".jpg";
outStream = new FileOutputStream(GetPathForFileName(fName));
outStream.write(data);
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
camera.startPreview();
}
private String GetPathForFileName(String fileName)
{
try
{
String savePath = GetSaveFolderFromConfiguration();
File file = new File(savePath + "/" + fileName);
return savePath + "/" + fileName;
}
catch (Exception ex)
{
ExceptionForm.ShowException(ex);
}
return null;
}
public static String GetSaveFolderFromConfiguration() throws Exception
{
String path = Environment.getExternalStorageDirectory().toString() + "/MediaFiles";
File f = new File(path);
if (f.exists())
return path;
else
f.mkdir();
return path;
}

Camera Preview Out of Memory Error

Objective of the program: the user is able to place one picture on the top of camera preview and move it freely (image is big 1920x1080 pixels) and can store this two layers (picture and camera preview) pressing the photo button. So far he is only able to save one picture of this kind because when he takes the second one the out of memory error problem appears.
Solution It seems that the problem can be solved if you use recycle bitmap function when you dont need to use bitmaps anymore. Or you can resize them (didn't want to)... Are these good ideas?
Code:
private PictureCallback mPicture = new PictureCallback()
{
#Override
public void onPictureTaken(byte[] data, Camera camera)
{
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
mPreview.setDrawingCacheEnabled(true);
mPreview.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_AUTO);
Bitmap bitmap = mPreview.getDrawingCache();
bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
combination = overlay(bmp, bitmap);
if(pictureFile == null)
{
return;
}
try
{
FileOutputStream fos = new FileOutputStream(pictureFile);
combination.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"+ mediaStorageDir)));
}
catch(FileNotFoundException e)
{
Log.d(TAG, "File not found: "+e.getMessage());
}
catch(IOException e)
{
Log.d(TAG, "Error accessing file: "+e.getMessage());
}
}
};
public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2)
{
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(),bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, null, new Rect(0,0,bmp1.getWidth(), bmp1.getHeight()), null);
return bmOverlay;
}
private static File getOutputMediaFile(int type)
{
File mediaFile = null;
if(isSdPresent() == false)
{
Log.d(TAG, "There is no Sd card. Cannot use the camera");
}
else
{
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "World Cup Camera");
if(!mediaStorageDir.exists())
{
if(!mediaStorageDir.mkdirs())
{
Log.d("WorldCupApp", "failed to create directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
if (type == MEDIA_TYPE_IMAGE)
{
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg");
}
else
{
return null;
}
}
return mediaFile;
}
I heard about this code should I use?
private Bitmap decodeFile(File f)
{
try
{
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//The new size we want to scale to
final int REQUIRED_SIZE=70;
//Find the correct scale value. It should be the power of 2.
int scale=1;
while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
scale*=2;
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
}
catch (FileNotFoundException e) {}
return null;
}
You can use android:largeHeap="true" inside the application tag on AndroidManifest when you're having outofmemory errors.

Categories

Resources