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();
Related
How to compress bitmap to jpeg? Storing in a file system is unacceptable.
Just use below code!
Bitmap bmp = null;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
I've converted an bitmap image into string to save it:
............
Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
Then I retrieve the bitmap from string to set an activity's background just like that:
byte[] temp = Base64.decode(encodedImage, Base64.DEFAULT);
Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0,
temp.length, options);
Drawable d = new BitmapDrawable(getResources(), bitmap);
getWindow().setBackgroundDrawable(d);
Everything works fine but the image quality reduces tremendously. I know it is because photo.compress(Bitmap.CompressFormat.JPEG, 100, baos); as I am compressing this image(though in best compressing resolution). So how can I do this without compressing the image? I have also tried the following code but it returns nothing
.......
Bitmap photo = extras.getParcelable("data");
int bytes = photo.getWidth() * photo.getHeight() * 4;
ByteBuffer buffer = ByteBuffer.allocate(bytes);
photo.copyPixelsToBuffer(buffer);
byte[] b = buffer.array();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
Also I am using sharedPreference to save the image. I thought of sqlite or internal storage also, but in both case compressing is needed as I found in internet
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();
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
I am trying to rotate an image from sdcard and then save back to sdcard.
I can do that for ".jpg" format by using ExifInterface class:
exif = new ExifInterface(filepath);
exif.setAttribute(ExifInterface.TAG_ORIENTATION, Integer.toString(orientation));
exif.saveAttributes();
For ".png" files, I would have to actually rotate and save:
Bitmap bitmap = BitmapFactory.decodeFile(filepath);
Matrix matrix = new Matrix();
matrix.postRotate(degrees);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
FileOutputStream stream = new FileOutputStream(fileLocation);
bitmap.compress(CompressFormat.PNG, 100, stream);
What about ".bmp", ".tiff", ".gif" ??
It seems like CompressFormat only supports 'CompressFormat.PNG' and 'CompressFormat.JPG'.
Is this limitation?
Yes Limited To JPG , PNG , WEBP
http://developer.android.com/reference/android/graphics/Bitmap.CompressFormat.html
Hey just give the name to .bmp
Do this:
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
_bitmapScaled.compress(Bitmap.CompressFormat.PNG, 40, bytes);
//you can create a new file name "test.BMP" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "**test.bmp**")
it'll sound that IM JUST FOOLING AROUND but try it once it'll get saved in bmp foramt..Cheers