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);
Related
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.
I have the following code for getting small sized avatar image:
Bitmap b= BitmapFactory.decodeFile(selectedImagePath);
File file = new File(selectedImagePath);
Bitmap out = Bitmap.createScaledBitmap(b, 128, 128, false);
FileOutputStream fOut;
try {
fOut = new FileOutputStream(file);
out.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
b.recycle();
out.recycle();
}
The problem is that after executing this code, my image in gallery is rescaled too, but I must leave it without any changes.
I tried to use copy of Bitmap image like this:
Bitmap bmp2 = b.copy(b.getConfig(), true);
Bitmap out = Bitmap.createScaledBitmap(bpm2, 128, 128, false);
or
Bitmap bmp2 = Bitmap.createBitmap(b);
Bitmap out = Bitmap.createScaledBitmap(bpm2, 128, 128, false
But my original image still changes. How could I obtain new, independent copy of this image?
You are loading the image from "selectedImagePath" and then your output file points to the same path, when you call compress() you overwrite your original image.
Try making a new name for the resized image.
fOut = new FileOutputStream(renamedFile);
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);
I am converting drawable resource png image to bitmap and then converting that bitmap to base64 and sending it to server by web service.
The image is stored on server at some address and in response i am getting URL where the image is stored.
The problem is after sending image to server the url which i am getting with that url i am setting image to other imageview but the transperent part of image is becoming black colored,
i think the problem is with converting png image to base64,
my code:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
bitmap = Bitmap.createScaledBitmap(bitmap, 300, 300, false);
mImageView.setImageBitmap(bitmap);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, bao);
byte[] ba = bao.toByteArray();
base64Image = Base64.encodeToString(ba, Base64.DEFAULT);
please give me solution ASAP
Everything you are doing seems right. Depending on the version of Android you are compiling with, decodeResource can have different default behavior. You can force the BitmapFactory to use an alpha channel when decoding with the following code.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inDither = false;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher, options);
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();