Bitmap to byteArray without compress - android

I am aware of the code below
Bitmap photo = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
but the issue is that this code reduces the image quality so much that it is unrecognizable how can i avoid
photo.compress(Bitmap.CompressFormat.PNG,100, stream);

You can use CompressFormat.PNG, where the compress quality doesn't matter, its always lossless as described in the documentation

array = new byte[w*h*4];
Buffer dst = ByteBuffer.wrap(array);
bmp.copyPixelsToBuffer(dst);

Related

Android: Efficient way to compress Bitmap

Is there a decent way to compress a Bitmap without re-scaling it without eating too much memory that cause OOM?
Bitmap compressedImageFile;
try {
compressedImageFile = MediaStore.Images.Media.getBitmap(getContentResolver(), fileUri);
//Converting bitmap to array stream
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//Decreasing the quality for thumbnail
compressedImageFile.compress(Bitmap.CompressFormat.PNG, 50, byteArrayOutputStream);
//Convert Bitmap to Byte array
byte[] thumbData = byteArrayOutputStream.toByteArray();
...
byteArrayOutputStream.toByteArray() is causing OOM, this can be avoid by reducing the bitmap size in a half but probably there is a more decent approach?
This is where I am using it

How to covert Image to byte[] or bitmap in Android Studio?

Can someone help me to solve a problem in Android Studio? I am creating an app in which I have a photo in JPEG format(Comming from the camera, not from file) and I want to convert it to bitmap.
You can use this method to convert image to byte[]. Like this:
public byte[] getBytesFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, stream);
return stream.toByteArray();
}
// Here image variable is a JPEG file/image.
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);

Bitmap to jpeg without saving to the file system

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

Bitmap compress doesn't changing bitmap byte size

I'm trying to reduce bitmap size by using compress method.
This is my code:
public Bitmap compressImage(Bitmap image) {
Bitmap immagex = image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Log.i("before compress", immagex.getByteCount()+"");
boolean compress = immagex.compress(Bitmap.CompressFormat.JPEG, 10, baos);
if(compress)
Log.i("after compress", immagex.getByteCount()+"");
else
Log.i("bad compress", "bad compress");
return immagex;
}
When i check my logs i get:
11-28 11:10:38.252: I/before compress(2429): 374544
11-28 11:10:38.262: I/after compress(2429): 374544
Why is the compress does not work?
UPDATE:
I tried this code:
public Bitmap compressImage(Bitmap image) {
Bitmap immagex = image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Log.i("before compress", immagex.getByteCount()+"");
boolean compress = immagex.compress(Bitmap.CompressFormat.JPEG, 10, baos);
Log.i("after compress 2", decodeSampledBitmapFromByte(image.getWidth(), image.getHeight(), baos.toByteArray()).getByteCount()+"");
return immagex;
}
Still the same byte count
11-28 11:33:04.335: I/before compress(3472): 374544
11-28 11:33:04.395: I/after compress 2(3472): 374544
Following is the code for reducing the bitmap size and convert it to base64,
Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, 50, 50, true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
newBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] byteArray = baos.toByteArray();
String imageEncoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
Log.e("Base 64 String", imageEncoded);
Using Bitmap.compress() you just specify compression algorithm and by the way compression operation takes rather big amount of time. If you need to play with sizes for reducing memory allocation for your image, you exactly need to use scaling of your image using Bitmap.Options, computing bitmap bounds at first and then decoding it to your specified size.
The best sample that I found on StackOverflow is this one.
Strange out of memory issue while loading an image to a Bitmap object
Compression does not change the size of the bitmap. You should look at the size of the output byte stream which contains compressed data.

How to get a byte array from a drawable resource ?

I would like to get a byte array from an jpeg image located in my res/drawable file ?
Does anyone know how to do that please ?
Drawable drawable;
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
Get a bitmap decodeResource(android.content.res.Resources, int)
Then either compress it to ByteArrayOutputStream() or copyPixelsToBuffer and get your array from the buffer.
http://developer.android.com/reference/android/graphics/Bitmap.html
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mPhoto.compress(Bitmap.CompressFormat.JPEG /* FileType */,
100 /* Ratio */, stream);
HTH !

Categories

Resources