Creating Image instance from drawable images in android - android

i am creating digital signature app using iTextG jar, to add water mark on the signature field, iText's
appearance.setImage(Image.getInstance(signedImagePath));
Requires a path of the watermark image, i want to use image from drawable folder of android, kindly suggest me i am not getting how to create Image instance from drawable, suggest if any other option is there?

You use ImageInstance something like this.
Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(),R.drawable.yourimage);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream);
Image myImg = Image.getInstance(stream.toByteArray());
Hope this helped.

http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeResource(android.content.res.Resources, int, android.graphics.BitmapFactory.Options)

Related

How to save images in .thumbnail folder with proper format?

I need to download images from server and store as thumbnails directly.
and for display images also i need to use images from .thumbnails folder directly. i am not getting how to create and save images as .thumbnail . and how to use images from that .thumbnail file.i searched online but everywhere only this below code present.
Bitmap thumb = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(file.getPath()), width, height);
any help?
Please check below code which helps you.
Follow below steps:
Calculate the maximum possible inSampleSize that still yields an image larger than your target.
Load the image using BitmapFactory.decodeFile(file, options), passing inSampleSize as an option.
Resize to the desired dimensions using Bitmap.createScaledBitmap().
Now you have your bitmap ready and you can save it any where using the following code
Bitmap thumbnail;
File thumbnailFile = ...;
FileOutputStream fos = new FileOutputStream(thumbnailFile);
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();
hope it helps you and save your time.
Or use Glide library which store image as a full image and 1 thmbnail image which is power caching library which loads image quickly and once download image after load image from cache not from network.
Below is a Glide link please read it advantages and features and after used into your applications.
https://github.com/bumptech/glide

How to make simple image to animated image like GIF?

I wont to make simple image to Animated image like GIF.I can select the image for gallery and store in image file.But, How i can modify image file to animated image or GIF File .
you have to select images you want to make GIF, then use bitmaps and run code in background Thread/Asynchtask:
Here i used Drawable, in your case convert images into bitmap and use it.
Bitmap one=BitmapFactory.decodeResource(getResources(),R.drawable.neon0);
Bitmap two=BitmapFactory.decodeResource(getResources(),R.drawable.neon1);
Bitmap three=BitmapFactory.decodeResource(getResources(),R.drawable.neon2);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
AnimatedGifEncoder encoder = new AnimatedGifEncoder();
encoder.start(bos);
encoder.addFrame(one);
encoder.addFrame(two);
encoder.addFrame(three);
encoder.finish();
FileOutputStream outStream;
try{
outStream = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() +
"/IMAGES_GIF/" + "animated.gif");
outStream.write(bos.toByteArray());
outStream.close();
}catch(Exception e){
e.printStackTrace();
}
Get Class from:
https://gist.githubusercontent.com/wasabeef/8785346/raw/53a15d99062a382690275ef5666174139b32edb5/AnimatedGifEncoder.java
This question needs a lot more information. Is the file already a GIF and it is not animating or are you trying to convert an image to a GIF? The latter is not possible, a GIF is a series of images.
You must create the GIF elsewhere and it is easiest to use a library like Glide (https://github.com/bumptech/glide) or Picasso (http://square.github.io/picasso/) to load the GIF.

what is the best way to work with Images in SqLite android?

I am working with a customizable database with pictures. Right now I am taking pictures as it is from the sdcard and encoding it in base64 String and then putting it in the database. but whenever I am trying decoding it and showing it in my view, I am getting Out of memory error. Can any one one tell me what is the best procedure to do it? Shall I change the size of the pictures before encoding it?
I want to re-size all of the pictures into 512*512.
Image to Base64 is very heavy operation in android. Consider saving the images on the external/internal memory and save the file path in the sqlite database.
You can convert your image to byte array then store values in sql by using BLOB type and vice versa.
As you mentioned you want to resize the images to 512*512, you can scale the image using below code,
Create bitmap from captured image and then use below line
Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, 512, 512, false);
It will give you a smaller image, you can also consider compressing the image to reduce in size,
OutputStream imagefile = new FileOutputStream("/your/file/name.jpg");
// Write 'bitmap' to file using JPEG and 50% quality hint for JPEG:
bitmap.compress(CompressFormat.JPEG, 50, imagefile);
Now, you have two options,
Save the scaled and compressed image into a file and save the path of that file in db. (Better way)
Convert the scaled and compressed image to base64 string and save in db.
Althought base64 is , as many answers said, a heavy operation for android, if done properly, it should not be a problem.
There are many reasons a bitmap could be required to be saved to a DB , (photo of a invoice ticket, for example?), and this is the way i do it.
first, create a new , smaller bitmap like #Swapnil commented.
and second, correctly use the bitmap transformation methods, i've been using these (look below) two so far and haven't had any memory issue on many different devices.
link to my BitmapUtils transformation methods

How to save Bitmap to .tiff in Android?

I know it is possible to save bitmap to png or jpg
now i want to save bitmap to .tiff,anybody can help?
Android does not have native support for saving TIFF files.
You best bet would be to get the raw byte array, and then using either a Java or NDK library (like libtiff) to save it as a TIFF.
I believe this is is a repost from here:
Convert a bitmap image to an uncompressed tif image in Java
To answer the question, the above answer suggested the following using the Java Advanced Imaging (JAI) library:
TIFFImageWriterSpi spi = new TIFFImageWriterSpi();
ImageWriter writer = spi.createWriterInstance();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_DISABLED);
ImageOutputStream ios = ImageIO.createImageOutputStream(new File("output.tif"));
writer.setOutput(ios);
writer.write(null, new IIOImage(bmp, null, null), param);
You always can save the Bitmap as .png and convert it to .tiff
Android hasn't support for tiff images. You should use some library for working with tiff. For example my: https://github.com/Beyka/Android-TiffBitmapFactory

What is wrong with saving image into sqlite database?

I want to write an image into database as blob type. But when i load this image from database, it's different from source image. I write source image into database as follow:
ByteArrayOutputStream bs = new ByteArrayOutputStream();
Bitmap medBmp = BitmapFactory.decodeStream(this.getResources().openRawResource(R.drawable.source_image));
medBmp.compress(Bitmap.CompressFormat.JPEG, 100, bs);
initialValues.put(IMAGE_COL,bs.toByteArray());
And here are images
source image:
Image is loaded from database
The background of source image is transparent, but the image is loaded from db has background in black.why are they different?What is wrong with my code?
Please help me out, thanks you so much.
You are saving the image as JPEG, and JPEG's don't support transparency. So, you would get that black background. Try using PNG format.

Categories

Resources