Reduce Image Size Using Compression Without changing Dimension - android

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();

Related

Camera2 API - jpeg capture file size too big

I'm using Camera2 api to do a still image capture and save it to a jpeg file. The problem is that the size of the file is always >900kb, even if I set the image dimensions to the smallest available and put jpeg quality low.
This is how I'm saving the file in the ImageAvailableListener. It's a xamarin project so code is in c#.
image = reader.AcquireLatestImage();
ByteBuffer buffer = image.GetPlanes()[0].Buffer;
byte[] bytes = new byte[buffer.Remaining()];
buffer.Get(bytes);
output = new FileOutputStream(File);
output.Write(bytes);
output.Close();
The file should be ~20kb, so why can't I get file sizes lower than 900kb?
You can also reduce the capture image quality
mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mPreviewRequestBuilder.set(CaptureRequest.JPEG_THUMBNAIL_QUALITY, (byte) 70); // add this line and set your own quality
I figured it out. Needed to create a bitmap to apply compression:
image = reader.AcquireLatestImage();
ByteBuffer buffer = image.GetPlanes()[0].Buffer;
byte[] bytes = new byte[buffer.Remaining()];
buffer.Get(bytes);
// need to get the bitmap in order to compress
Bitmap bitmap = BitmapFactory.DecodeByteArray(bytes, 0, bytes.Length);
using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 85, stream);
Save(stream.GetBuffer());
}

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.

bmpFactoryOptions don't work, no idea why

I use the following code to get a Blob back from my SQLite database and get back a bitmap. My problem is that the reconstructed bitmap is larger than the original picture (input). It seems that my BitmapFactory.Options isn't working, but I have no idea what is wrong, nor am I getting an error. What is wrong with this code?
byte[] blob = contact.getMP();
ByteArrayInputStream inputStream = new ByteArrayInputStream(blob);
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
bmpFactoryOptions.inScaled = false;
bmpFactoryOptions.outHeight = 240;
bmpFactoryOptions.outWidth = 320;
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, bmpFactoryOptions);
try {
FileOutputStream out = new FileOutputStream("mnt/sdcard/test5.png"); bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
e.printStackTrace();
}
When I check the output my 320 x 240 picture is 427 x 320. I don't want to use Bitmap.createScaledBitmap because it messes up the quality.
you have to pass to decodeStream in order to work
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, bmpFactoryOptions);

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();

Android - take a picture of small

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

Categories

Resources