How can I create a YuvImage in android - android

I need to create a yuv image in NV21 format for testing purpose.
From this posting, Displaying YUV Image in Android, I see it does:
YuvImage yuvImage = new YuvImage(data, PictureFormat.NV21, width,
height, null);
How can I get 'data'? to passing in yuvImage?
Can I load it from a resource file, what should be the resource file format?

You can get your desired Parameter (data) by Converting the resource Drawable to Bytearray then use it to get the YUV image :
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.my_pic);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitMapData = stream.toByteArray();

Related

Binary Request Body in kotlin

How Can i set image from imageview in binary body in API endpoint?
I need to get image from an ImageView and put it into a request body that expects a binary value (jpg image)
How can I do the bellow API calling in kotlin?
This is the call in postman
There isn't enough context in your question, eg example code. But try the below:
//get drawable from your imageview
Drawabel drawable = imageVIew.getDrawable();
BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable);
//convert image to binary
Bitmap bitmap = bitmapDrawable.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte);
byte[] imageInByte is what you asked for, but I have also added conversion to stream in case you end up needing that.

Dart/Flutter Image library decode Bitmap fails

I am trying to read a bitmap generated on the platform site
here is the code I use to generate a byte array in android:
ByteBuffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());
bitmap.copyPixelsToBuffer(byteBuffer);
return byteBuffer.array();
When I try to decode it with the image library it fails (dart code):
Image img = decodeBmp(bitmapData);
The method 'getBytes' was called on null.
bitmap data is not empty and looks proper for me
If I instead compress image on the android side to png or jpg than decodeImage (or -Png or -Jpg) works
but I dont want to use bitmap.compress
this code would work with compress
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bufferedBmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imageData = baos.toByteArray();
return imageData;

Byte array to bitmap converting issue

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

Pass Drawable to iTextPdf instead of an Image - Android

I am using iTextPdf to create a check-in application. I've added the an image already using the image object :
imagePath = "/sdcard/Mugshot.jpg";
Image image = Image.getInstance(imagePath);
image.setAbsolutePosition(165f, 465f);
image.scaleToFit(290f,290f);
document.add(image);
I would prefer to add the image in the same way before adding any content, as I expect any subsequent stuff would write over existing stuff.
As per the API Documentation of iTextPdf you can also contruct using byte[] array
Convert drawable to byte[]
Drawable d = getResources ().getDrawable (R.drawable.your_drawable)
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitmapData = stream.toByteArray();
Then
Image image = Image.getInstance(bitmapData);

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