How to make bitmap bigger without resizing the actual image - java

I am using Pax A920 device.
I have an issue when printing a bitmap, there is no option to center the bitmap(image) documentation
Methods reference:
The issue is when the bitmap's width is short, its left-aligned on the printed receipt.
I can scale the bitmap using:
Bitmap.createScaledBitmap(receiptLogo, 512, receiptLogo.getHeight(), false);
But it looks bad, I'm thinking of creating a new Bitmap with width of 512 & height of receipt.Logo.getHeight() then put the receipt logo in the middle so it wont look stretch.
Is this doable?

Related

Drawing an image using its full size rather than asking me for its dimensions in JavaFX

I am loading an external image with JavaFX:
Image tile = new Image(imageFile.toURI().toURL().toString(),width, height, true,true);
You need to supply the width and height. How can I get the image's width and height?
Now, I know that the width and height of a PNG image are determined by the bytes from 12 to 20 or something like that, so technically you could open a byte stream and interpret the bytes. However, this seems overkill for the simple task of drawing an image in my JavaFX application. Is there not a way to draw an image with its full size without asking me to supply the dimensions?
Just create the Image using a constructor which does not include dimensions (e.g. new Image(url)):
Image image = new Image(url);

ImageView doesn't stretch image properly

So I have no clue why this is happening. I am using Universal Image Loader to load these images. It seems like the last line of pixels is being streched for some weird reason. I want the image to just stretch out evenly. (I don't care that it will look weird. The images below are for demo purposes.)
Also, don't mind the first and last image. I purposely blurred that out because it had someone's face on it.
This is how I set up my Universal Image Loader:
//setup Image Loader for loading cruise line logos
displayImageOptions = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher)//show this image when image is loading
.showImageForEmptyUri(R.drawable.ic_launcher)//show this image incase image doesn't exist
.showImageOnFail(R.drawable.ic_launcher)//show this image if fetching image from URL didn't work
.cacheInMemory(true)//cache image in RAM
.cacheOnDisc(true)//cache image in device for later use
.considerExifParams(true)
.displayer(new RoundedBitmapDisplayer(5))//super subtle rounded corners on images
.build();
This is caused by the way RoundedBitmapDisplayer draws the bitmap.
If you look at the source, you'll see that it uses a RoundedDrawable, which uses canvas.drawRoundRect() to draw a rounded rectangle of the desired size of the Drawable, using the downloaded image as the texture via a BitmapShader. BitmapShader does not support scaling (only clamping and tile modes). Try using a SimpleBitmapDisplayer instead which uses the normal ImageView.setImageBitmap() way of displaying the image.
If you need rounded corners, you'll have to find a different way to implement that, for example by scaling the Bitmap to the desired size first. Another option is to call Canvas.saveLayer() before delegating to BitmapDrawable for the scaling, and then applying the rounded corner masking effect using PorterDuff.Mode.DST_IN. Either way you'll end up writing a bit more low-level code, but you should be able to encapsulate everything nicely in a custom BitmapDisplayer.

How generate images with different resolution for android project?

I have a lot of images with xhdpi resolution. Now I need the same images but in mdpi, ldpi, hdpi resoulution. I think this process could be automated. So, what is the best way to generate images with different resolution?
Try using:
http://android-ui-utils.googlecode.com/hg/asset-studio/dist/index.html
This helps a lot in generating resources. Hope it helps!
The process is already automated. If you provide just one size - hdpi works for me - then the Android system will resize or rescale it as needed for other screen resolutions and sizes.
The drawback of not providing images in different sizes is that the automatic resizing algorithm might not do as good a job as a graphic designer. However, if you are looking for a non Android algorithm to do the resizing, who's to say it will be any better than the built-in algorithm?
I suggest trying just one size on various devices and see how your graphics looks. Some images will scale better than others, so you may only need to provide multiples for a few.
Not too long ago I needed to resize a Image in java and I found a very good method for that, it returns a type of bufferedimage and you will have to use that new instance. You can specicify your width and height. Here is the code:
public BufferedImage resizeImage(final Image image, int width, int height) {
final BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
final Graphics2D graphics2D = bufferedImage.createGraphics();
graphics2D.setComposite(AlphaComposite.Src);
graphics2D.drawImage(image, 0, 0, width, height, null);
graphics2D.dispose();
return bufferedImage;
}
Just putting in 1 image at a higher resolution (like xhdpi or hdpi) and using that for all scales is not efficient as while android does resize it to fit, it still is loading the image in its original size into memory, thus this would cause some lag or outOfMemory errors if you have lot of them in 1 activity.
I would use the bitmap Image loader that google recommends.
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#decodeSampledBitmapFromResource
This lets you take an image, and it will resize it, while only loading the new image size into memory. This is also the method you would want to do if you want a "lazy load".
The solution I chose to handle all my drawables is the following:
1- Have all drawables in high resolution in a /etc folder in your app project.
For the high resolution I chose 10 times the default resolution (mdpi). So for instance the launcher icon drawables (48dip x 48dip which is 48 x 48 pixels in mdpi) would be 480 x 480 pixels.
I have all my drawables in high res in the /etc folder.
2- At build time, my ant script would take all drawables from /etc, and resize them to fill all /res/drawables-* folders.
From the highres drawable at 100% size, the various resolutions sizes will be: ldpi: 7.5%, mdpi: 10%, hdpi: 15%, xhdpi: 20%, xxhdpi: 30%
I use an external program launched by ant to actually do the resizing.

Bitmap quality lost after scaling canvas

I want to paint a Bitmap scaled in a canvas (based on the Bitmap and the View size). The problem is that if I scale the canvas before painting the bitmap:
canvas.scale(1.5f,1.5f);
canvas.drawBitmap(mBitmap, 0, 0, paint);
This is the result:
But if instead of scaling the canvas I scale the bitmap:
Bitmap scaled = Bitmap.createScaledBitmap(
mBitmap, bitmapWidth, bitmapHeight,
true);
canvas.drawBitmap(scaledBitmap, 0, 0, paint);
The result is much better:
The problem is that the Bitmap is pretty big and having in memory both the original and the scaled bitmap may be problematic.
Is there any way to obtain the second result without creating a scaled Bitmap every time the Bitmap or the View bounds changes?
Use a matrix that contains the scaling info when drawing to canvas
canvas.drawBitmap(x,y,..., matrix...)
Use inSampleSize in BitmapOptions when reading the bitmap from your source to scale it accordingly (it is passed as an extra parameter).
This is androids preferred way of loading images in nearly all cases to avoid memory overflow, as the full image is Never loaded into memory.
The android docs on Loading BItmaps efficiently can be found here.
With example code on how to retrieve an image of desired size from your source. (resource, file, etc etc)
The two methods in Loading Large Bitmaps Efficiently are the ones you are looking for.
The solution to my problem was that the Paint hasn't enabled the FILTER_BITMAP_FLAG flag.

Fit imported image in panel

I tried importing image via JFileChooser into JPanel. It worked. but I need to fit the image inside the panel without losing its aspect ratio or its proportion.
I tried rescaling the image, here's my code:
Image img = Toolkit.getDefaultToolkit().createImage(picture.getSource());
Image scaledImage = img.getScaledInstance(jPanel1.getWidth(),jPanel1.getHeight(),Image.SCALE_SMOOTH);
g2.drawImage(scaledImage, 0, 0,null,null);
But unable to protect its ratio. I need a simple code for this.
Try reading the instructions:
If either width or height is a negative number then a value is substituted to maintain the aspect ratio of the original image dimensions. If both width and height are negative, then the original image dimensions are used.

Categories