I have a ByteArrayOutputStream I have created using the QRGen Qr code generation library, and I want to turn it into a BufferedImage object and present it in an ImageView in Android. How may that be achieved?
Convert the ByteArrayOutputStream to Byte array
byte[] data = baos.toByteArray(); //baos is your ByteArrayOutputStream object
them create a bitmap out of it using the BitmapFactory
Bitmap bmp = BitmapFactory.decodeByteArray (data,0,data.length, null);
them take your ImageView and set its bitmap
imageView.setImageBitmap(bmp);
Related
Before I was using MVVM architecture and Kotlin I done it like this in Java.
I am passing image Uri to Bitmap
Java code:
Bitmap actualImage1 = BitmapFactory.decodeStream(getContentResolver().openInputStream(mImageUri));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
actualImage1.compress(Bitmap.CompressFormat.JPEG, 30, baos);
byte[] finalImage = baos.toByteArray();
getContentResolver() is not recognized by Kotlin.
How to write this in Kotlin, again I have image uri that I want to pass to Bitmap
I do it like this in Kotlin
val bitmap = MediaStore.Images.Media.getBitmap(
this.contentResolver,
selectedPhotoUri
)
I'm trying to encode an animated gif to base64, so I can upload it to server. I am already doing it with static images, but if I try to do it with GIF, it simply does not work. I get the image from Intent image picker and put it inside an ImageView.
Here is the code I use to encode images to base64:
Bitmap bitmap = ((BitmapDrawable) ((ImageView) nextChild).getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, baos);
byte[] imginbyte = baos.toByteArray();
value = Base64.encodeToString(imginbyte, Base64.DEFAULT);
try this :
byte[] imginbyte = Base64.decodeBase64(bitmap.getImageBase64());
new FileOutputStream("image2.gif");
write(imginbyte);
close();
i want get image From server and put to this image view i want get image from this imageview and compress with bitmap and send this picture to other fragment i dont want add 2 request for my server
my code is :
Bitmap bmp = BitmapFactory.decodeResource(getResources(),"my problem");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
mBundle.putByteArray("ResID", byteArray);
i know if i put R.darwable.ic_luncher it will work
but i want take this image from image view and put to " my probem "
can u help me ?
You can use mImageView.getDrawable() to get Drawable object from ImageView:
Drawable drawable = mImageView.getDrawable();
After that you can obtain a Bitmap object from it:
BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable);
Bitmap bitmap = bitmapDrawable.getBitmap();
and further, if you want, even InputStream for byte array to send it:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Here, the CompressFormat and quality can be adjusted for output image size
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageBytes = stream.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);
As a result, imageBytes will contain all the bytes of the image, which you can send over elsewhere (in your scenario: to your other Fragment)
I have an image that I encode using this code :
Bitmap bm = BitmapFactory.decodeFile(selectedImagePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
It encodes well. After that I'm getting the String in a file in my computer and I try to decode it with this website :
http://www.askapache.com/online-tools/base64-image-converter/
And I'm getting a null picture. However, when I decode it directly in my app and display it like :
public static Bitmap decodeBase64(String input) {
byte[] decodedByte = Base64.decode(input, 0);
return BitmapFactory
.decodeByteArray(decodedByte, 0, decodedByte.length);
}
private ImageView img;
img.setImageBitmap(decodeBase64(encodedImage));
I'm getting the right picture. So I was wondering if i actually can't decode an android encoded image with an other decoder than android's one or my file is corrupted meaning that I have missing characters. However even if I don't have the whole encoded file, the decoded picture i'm getting with the website should look like to the good one right ?
Thank you in advance
I use following code for get bitmap from string but i get null bitmap. So please guide me.
byte[] Image_getByte = Base64.decode(img);
ByteArrayInputStream bytes = new ByteArrayInputStream(Image_getByte);
BitmapDrawable bmd = new BitmapDrawable(bytes);
Bitmap bitmap=bmd.getBitmap();
Log.v("log","Home bitmap "+bitmap);
i.setImageBitmap(bitmap);
use BitmapFactory.decodeByteArray(Image_getByte)
here you don't need any String, after getting the byte array, just pass it in the mentioned method which returns Bitmap
The javadoc for getBitmap() says:
"Returns the bitmap used by this drawable to render. May be null."
If img is base 64 String use the following source to get the bitmap from base 64 string.
byte[] source=img.getBytes();
byte[] Image_getByte = Base64.decode(source);
Bitmap bitmap = BitmapFactory.decodeByteArray(Image_getByte, 0,Image_getByte.length);
imageView.setImageBitmap(bitmap);
This may help you
Please consider 'ImageContents' as String which contains image data.
byte[] imageAsBytes = Base64.decode(ImageContents.getBytes());
ImageView image = (ImageView)this.findViewById(R.id.ImageView);
image.setImageBitmap(
BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
);
ImageView :In Android, you can use “android.widget.ImageView” class to
display an image file
BitmapFactory:Creates Bitmap objects from various sources, including
files, streams, and byte-arrays.
For more information about BitmapFactory see here