When you have an image (PNG in this case) saved as byte array on android, how can scale it to get a new image scaled in byte array format?
Have in mind that the image to scaling is to a smaller size, avoiding loss of data.
Image croping byte uri image convert byte array the step
InputStream iStream = getContentResolver().openInputStream(uri);
byte[] inputData = getBytes(iStream);
One quick way of doing this, it's letting Android to resolve the algorithm for us.
So, we convert the byte array to a bitmap, the bitmap can create a new bitmap with new sizes defined and finally convert back to byte array.
private byte[] getScaledImage(byte[] originalImage, int newWidth, int newHeight) {
// PNG has not losses, it just ignores this field when compressing
final int COMPRESS_QUALITY = 0;
// Get the bitmap from byte array since, the bitmap has the the resize function
Bitmap bitmapImage = (BitmapFactory.decodeByteArray(originalImage, 0, originalImage.length));
// New bitmap with the correct size, may not return a null object
Bitmap mutableBitmapImage = Bitmap.createScaledBitmap(bitmapImage,newWidth, newHeight, false);
// Get the byte array from tbe bitmap to be returned
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
mutableBitmapImage.compress(Bitmap.CompressFormat.PNG, 0 , outputStream);
if (mutableBitmapImage != bitmapImage) {
mutableBitmapImage.recycle();
} // else they are the same, just recycle once
bitmapImage.recycle();
return outputStream.toByteArray();
}
Related
I am trying to implement Converting bitmap to byteArray android. I need to convert my byte[] to bitmap without compression. But everytime im getting whole black image. How to do it?
What I am doing:
int bytes = bmp.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
bmp.copyPixelsToBuffer(buffer);
byte[] resarray = buffer.array();
And here how I get it to bitmap:
BitmapFactory.Options options = new
BitmapFactory.Options();
options.inScaled = false;
Bitmap bmp = BitmapFactory.decodeByteArray(barray,0, barray.length,options);
ImageView imageView = (ImageView) findViewById(R.id.resdetayimage);
imageView.setImageBitmap(bmp);`
EDIT
Bitmap factory decode only compressed things. Thats why my code not work. So I need something like:
Bitmap.Config configBmp = Bitmap.Config.valueOf(bitmap.getConfig().name());
Bitmap bitmap_tmp = Bitmap.createBitmap(width, height, configBmp);
ByteBuffer buffer = ByteBuffer.wrap(byteArray);
bitmap_tmp.copyPixelsFromBuffer(buffer);
But the code still not working. How can I implement this ? I got byte[] from intent.
Convert bitmap to byteArray
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, bStream);
byte[] mByteArray = bStream.toByteArray();
Convert byteArray to Bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(mByteArray , 0, mByteArray.length);
Use copyPixelsFromBuffer() as it is the reverse of copyPixelsToBuffer().
I have an image which I turn into a byte array. I then encrypt this byte array with AES and I want a visual result to display representing this encryption.
The problem is all the header and meta information is also encrypted so this encrypted byte array, which is passed to byteToImage(), is not recognized as a valid representation of an image i.e. decodeByteArray() returns null.
I have tried cutting off the first 512 bytes of the original image and appending that back on to the start of the encrypted byte array in the hope that this will restore the header information - but it hasn't worked. I have this with .png and .bmp images. What I ideally want is a way to represent a RAW image in android and encrypt this information byte for byte - leaving out the need to fiddle around with headers etc.
I would really appreciate any help.
private static byte[] imageToBytes(ImageView iv){
byte[] imageInByte = null;
Bitmap originalImage;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable();
originalImage = drawable.getBitmap();
originalImage.compress(Bitmap.CompressFormat.PNG, 0, baos); // was 70
imageInByte = baos.toByteArray();
return imageInByte;
}
private static Bitmap bytesToImage(byte data[]) {
// byte[] x = Base64.decode(data, Base64.DEFAULT); using x in place of data also fails
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
return bmp;
}
Your idea will not work. If you encrypt just a lot of bytes of a bitmap byte array you will demolish its structure and after that you don't have a bitmap anymore. So you cannot display it. If you want to be able to display the 'encrypted' image then do it pixel wise. Have a look at Bitmap.getPixel()/setPixel() or Bitmap.getPixels()/setPixels().
I am facing one real issue. I need to convert image into byte array format, so that I can upload the byte array into web server. I have tried a lot however its not working. I am also getting negative values for byte array. I am not sure what i am doing wrong to take byte values in array.
Below is my code. Please help me what i am doing wrong?
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.home_menu_icon);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
Here i show code for both if your image is from GALLERY and CAMERA
if (requestCode == IMAGE_PICKER_REQUEST && resultCode == RESULT_OK) {
fileName = getRealPathFromURI(data.getData());
try {
if (bitmap != null) {
bitmap.recycle();
}
InputStream stream = getContentResolver().openInputStream(
data.getData());
bitmap = BitmapFactory.decodeStream(stream);
stream.close();
image.setImageBitmap(bitmap);
//picNameText.setText("Selected: en"
// + getStringNameFromRealPath(fileName));
ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream1);
imageInByte = stream1.toByteArray();
}catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
if (requestCode == IMAGE_TAKER_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
image.setImageBitmap(photo);
ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream2);
imageInByte = stream2.toByteArray();
}
enjoy it work for me.....
Try following code.
int bytes = bitmap.getByteCount();
//Create a new buffer
ByteBuffer buffer = ByteBuffer.allocate(bytes);
//Move the byte data to the buffer
b.copyPixelsToBuffer(buffer);
byte[] array = buffer.array();
A 32-bit Bitmap use a int32 to save a ARGB color, and we can use a
int array to represent a bitmap. Using Bitmap.getPixels() and
Bitmap.setPixels() to convert a Bitmap to int array and vise
verse. And a int array can be easily convert to byte array.
#Chintan Rathod also show a good solution by using Bitmap.copyPixelsToBuffer(), nearly the same with the first one.
Since your goal is to upload the bitmap to server, send a compressed
file is the best solution. Your code is just doing the right thing.
You said that you are getting negative values in byte array, which
is not an error. Just upload the byte array to server and save it as
a jpeg file.
A 32-bit Bitmap use a int32 to save a ARGB color, and we can use a int array to represent a bitmap. Using Bitmap.getPixels() and Bitmap.setPixels() to convert a Bitmap to int array and vise verse. And a int array can be easily convert to byte array.
I have a bitmap that I want to send to the server by encoding it to base64 but I do not want to compress the image in either png or jpeg.
Now what I was previously doing was.
ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
bitmapPicture.compress(Bitmap.CompressFormat.PNG, COMPRESSION_QUALITY, byteArrayBitmapStream);
byte[] b = byteArrayBitmapStream.toByteArray();
//then simple encoding to base64 and off to server
encodedImage = Base64.encodeToString(b, Base64.NO_WRAP);
Now I just dont want to use any compression nor any format plain simple byte[] from bitmap that I can encode and send to the server.
Any pointers?
You can use copyPixelsToBuffer() to move the pixel data to a Buffer, or you can use getPixels() and then convert the integers to bytes with bit-shifting.
copyPixelsToBuffer() is probably what you'll want to use, so here is an example on how you can use it:
//b is the Bitmap
//calculate how many bytes our image consists of.
int bytes = b.getByteCount();
//or we can calculate bytes this way. Use a different value than 4 if you don't use 32bit images.
//int bytes = b.getWidth()*b.getHeight()*4;
ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
b.copyPixelsToBuffer(buffer); //Move the byte data to the buffer
byte[] array = buffer.array(); //Get the underlying array containing the data.
instead of the following line in #jave answer:
int bytes = b.getByteCount();
Use the following line and function:
int bytes = byteSizeOf(b);
protected int byteSizeOf(Bitmap data) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) {
return data.getRowBytes() * data.getHeight();
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return data.getByteCount();
} else {
return data.getAllocationByteCount();
}
BitmapCompat.getAllocationByteCount(bitmap);
is helpful to find the required size of the ByteBuffer
I want to store image in SQLite DataBase.
I tried to store it using BLOB and String, in both cases it store the
image and can retrieve it but when i convert it to Bitmap using
BitmapFactory.decodeByteArray(...) it return null.
I have used this code, but it returns null
Bitmap bitmap = BitmapFactory.decodeByteArray(blob, 0, blob.length);
Just try this:
Bitmap bitmap = BitmapFactory.decodeFile("/path/images/image.jpg");
ByteArrayOutputStream blob = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /* Ignored for PNGs */, blob);
byte[] bitmapdata = blob.toByteArray();
If bitmapdata is the byte array then getting Bitmap is done like this:
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);
Returns the decoded Bitmap, or null if the image could not be decoded.
The answer of Uttam didnt work for me. I just got null when I do:
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);
In my case, bitmapdata only has the buffer of the pixels, so it is imposible for the function decodeByteArray to guess which the width, the height and the color bits use. So I tried this and it worked:
//Create bitmap with width, height, and 4 bytes color (RGBA)
Bitmap bmp = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.ARGB_8888);
ByteBuffer buffer = ByteBuffer.wrap(bitmapdata);
bmp.copyPixelsFromBuffer(buffer);
Check https://developer.android.com/reference/android/graphics/Bitmap.Config.html for different color options