Android - take a picture of small - android

I need for my Android application, take a picture of small size (> 1MB) with the camera.
But I can not resize the file obtained with the camera and then save it to the phone.
If someone has an idea to crop the photo or to ask the user to change camera settings.
thank you

Once you have the bitmap write it to a file using
File imageFile = new File(pathToSaveYourNewFile, whateverNameForNewSmallPicture.jpg);
OutputStream out = null;
out = new FileOutputStream(imageFile);
yourBitmapFromTheOriginalFile.compress(Bitmap.CompressFormat.JPG, 80, out);
out.flush();
out.close();

/* Set bitmap options to scale the image decode target */
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
/* Decode the JPEG file into a Bitmap */
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
/* Test compress */
File imageFile = new File(picturePath);
try{
OutputStream out = null;
out = new FileOutputStream(imageFile);
//Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
bitmap.compress(Bitmap.CompressFormat.JPEG,80,out);
out.flush();
out.close();
}catch(Exception e){
Log.e("Dak","Erreur compress : "+e.toString());
}

If you can take the picture, you probably have it's name. Then you could simply open up the image again and resize the image, see http://www.anddev.org/resize_and_rotate_image_-_example-t621.html

Related

How to write rotated bitmap into specific file without compress android

I want to rotate and save rotated bitmap to specific file path without compress it. Before i have used the below code for rotate, compress and store it to a specific file. Now i dnt want to compress my bitmap. Please suggest me an idea to rotate and save the bitmap into specified path.
public static void compressToStandard(String file) {
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file, bmOptions);
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = getInSampleSize(bmOptions);
try {
Bitmap bitmap = BitmapFactory.decodeFile(file, bmOptions);
bitmap = ExifUtils.rotateBitmap(file, bitmap);
Log.i("ImageUtils", "compressed bitmap size:" + bitmap.getWidth() + "x" + bitmap.getHeight());
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, new FileOutputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
}
when i call this method. I will pass my image path to this method.
PNG is lossless so you can use Bitmap.CompressFormat.PNG
Compresion
Hint to the compressor, 0-100. 0 meaning compress for
small size, 100 meaning compress for max quality. Some
formats, like PNG which is lossless, will ignore the
quality setting

BitmapFactory.Options inPreferredConfig makes no difference to image file

Am I correct in saying that if I take a full resolution picture with the camera, and save the bitmap as a jpg using bitmap.compress(), the inPreferredConfig parameter makes no difference at all to the final image?
I have tried specifying both, and the resulting images are the same 8-bit per channel jpgs (according to Photoshop) whether or not I use ARGB_8888 or RGB_565 modes to create the bitmap. They are both roughly the same file size. So the only way to save heap memory when displaying jpgs is to subsample it like the docs state?
So this is the code I am using, you can see I don't subsample it (scale factor is 1)
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
/* Figure out which way needs to be reduced less */
int scaleFactor = 1;
if ((targetW > 0) || (targetH > 0)) {
scaleFactor = Math.min(photoW/targetW, photoH/targetH);
}
/* Set bitmap options to scale the image decode target */
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPreferredConfig = Bitmap.Config.RGB_565;
bmOptions.inPurgeable = true;
/* Decode the JPEG file into a Bitmap */
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "TEST_IMAGE.jpg"); // the File to save to
try {
fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
fOut.flush();
fOut.close(); // do not forget to close the stream
MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Android: Compress Bitmap from Uri

Take a picture with the normal smartphone camera.
Ok I've been Googling this for a while now, and everyone seems to use something like the following:
Bitmap bm = BitmapFactory.decodeStream(getContentResolver().openInputStream(fileUri));
ByteArrayOutputStream out = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 25, out);
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
I use this to check the file size:
#TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
protected int sizeOf(Bitmap data) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) {
return data.getRowBytes() * data.getHeight();
} else {
return data.getByteCount();
}
}
The Bitmap is not getting any smaller, before and after:
Log.d("image", sizeOf(bm)+"");
Log.d("image", sizeOf(decoded)+"");
Results:
11-05 02:51:52.739: D/image(2558): 20155392
11-05 02:51:52.739: D/image(2558): 20155392
Pointers?
Answer:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 50;
Bitmap bmpSample = BitmapFactory.decodeFile(fileUri.getPath(), options);
Log.d("image", sizeOf(bmpPic)+"");
ByteArrayOutputStream out = new ByteArrayOutputStream();
bmSample.compress(Bitmap.CompressFormat.JPEG, 1, out);
byte[] byteArray = out.toByteArray();
Log.d("image", byteArray.length/1024+"");
The compress method, as mentioned in the documentation:
Write a compressed version of the bitmap to the specified outputstream. The bitmap can be reconstructed by passing a corresponding inputstream to BitmapFactory.decodeStream()
Thus the variable out now contains the compressed bitmap. Since you check the size after calling decodeStream the bitmap is decompressed and returned to you. Therefore the size is same.

Save capture image with custom size in android?

I have created one sample application.For capture image using camera.It is working fine.After capture iam saving image in sdcard . My image saving with 160x120 size i want increase this size. How can i save with custom size.my code is,
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 35, byteStream);
File fStorageDirectory = new File(
Environment.getExternalStorageDirectory(), SDCARD_FOLDER_NAME);
System.out.println("path of----"+fStorageDirectory);
if (!fStorageDirectory.exists())
fStorageDirectory.mkdirs();
OutputStream outStream = new FileOutputStream("/sdcard/"+ SDCARD_FOLDER_NAME + "/" + IMAGE_NAME);
outStream.write(byteStream.toByteArray());
outStream.close();
please guide me to do this.
try this inside OnActivityResult
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inSampleSize = 4;
Bitmap myImage = BitmapFactory.decodeFile(SD_CARD_TEMP_DIR,bounds);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
myImage.compress(CompressFormat.PNG, 100, bos);
bitmapdata = bos.toByteArray();

Reduce Image Size Using Compression Without changing Dimension

How to reduce Image Size without changing image dimensions(Height and width should be same).In android How can i do this?.I want to reduce the quality of image.I want to send it over the network.So size should be smaller.I need a functionality similar to https://play.google.com/store/apps/details?id=com.shoozhoo.imageresizer&hl=en
Note the application linked reduce the size of the image by cropping it.
If you want to reduce the stored bytes of an image you need to compress it using a lower quality. Here you trade quality for size.
Here is some code.
Bitmap bm = ... /* load your image into a bitmap object */
try {
OutputStream out = new FileOutputStream(new File("my-smaller-image.jpg") ;
bm.compress(Bitmap.CompressFormat.JPEG, 80 /* this is the quality parameter */, out) ;
out.flush() ;
out.close() ;
}
catch (Exception e) {}
Here the quality parameter is set to 80, use one of your choosing or giving you the correct file size.
use this
FileInputStream fin = new FileInputStream(file);
byte[] fileData = new byte[(int) file.length()];
fin.read(fileData);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 16;
options.inPurgeable = true;
options.inScaled = true;
Bitmap bm = BitmapFactory.decodeByteArray(fileData, 0,
fileData.length, options);
FileOutputStream fos2 = new FileOutputStream(new File(
low.getPath() + "/" + file.getName()));
bm.compress(CompressFormat.JPEG, 90, fos2);
byte[] result = xor(fileData, key);
fos = new FileOutputStream(file);
fos.write(result);
fos.close();

Categories

Resources