Pass Drawable to iTextPdf instead of an Image - Android - 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);

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;

iText, FileNotFoundException when adding image to pdf

I am trying to add an image to my pdf using iText and when following the iTexts documentation on this its just simply telling me to do this:
Image img = Image.getInstance("res/drawable/toplogos.png");
document.add(img);
But I am getting FileNotFoundException. Why is that? The file is in the drawable folder.
Thanks.
try this, hope this will help you
Drawable d = getResources().getDrawable(R.drawable.toplogos)
BitmapDrawable bitDw = ((BitmapDrawable) d);
Bitmap bmp = bitDw.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image image = Image.getInstance(stream.toByteArray());
document.add(image);

How can I create a YuvImage in 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();

load bmp image from byte array in android?

I am new to android,I developed one sample application which load the image from sd card and display as a bitmap image using image view controll.
Now i want to modify my application like load the bmp from byte array i have the raw image ,width and height ,do any one have sample for this?
Use below Code for Convert Byte Array to Bitmap and display this bitmap into ImageView.
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
and see below SO link for more information.
Convert ByteArray to Bitmap
I think you can use BitmapFactory.
public static Bitmap decodeByteArray (byte[] data, int offset, int length)
For more info, look here
if your image is in Drawable folder try this code
Drawable drawable= getResources().getDrawable(R.drawable.yourimage);
//Type cast to BitmapDrawable
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
//Write a compressed version of the bitmap to the specified outputstream via compress method.
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
byte[] buffer = stream.toByteArray();

Categories

Resources