How to reduce the Image size? - java

loc =d_path+"\\a"+m+"a"+".jpg";
Image image = Image.getInstance (loc);
I want to reduce image width. Could anyone help me out for the same?

Image has method
public Image getScaledInstance(int width, int height, int hints)

Related

Is there a way to scale an image down to a specific size?

I am currently trying to get some kind of 2D dungeoncrawler (think: roguelike) running. Now I want to work with square tiles (32x32) but I wonder if there's a way to make my textures in a higher resolution, say 64x64, and scale them down onto a 32x32 square?
I imagine there has to be since almost all games do this in one way or another but all I can seem to find online is about 3D stuff.
Yeah. When you draw an image, you can add the new width and height to it to resize it.
public static BufferedImage resizeImage(BufferedImage image, int newwidth, int newheight) {
BufferedImage image2 = new BufferedImage(newwidth, newheight, BufferedImage.TYPE_INT_ARGB);
Graphics g = image2.getGraphics();
g.drawImage(image, 0, 0, newwidth, newheight, null);
g.dispose();
return image2;
}
Refer to here for more info.

android draw on a canvas a circle of a bitmap keeping aspect ration

I have a source bitmap of 300x100. I want to make a circle of 300x300 from this bitmap BUT i want to keep the aspect ratio. so the idea is to resize the source bitmap to 900*300 and them crop the source bitmap to 300x300 FROM THE MIDDLE (so remove 300 px from the top and remove 300 px from the bottom) so that at the end i have a square of 300x300 on with i can extract the circle.
is their anyone who can explain me how to do that ?
here is the Bitmap Class Bitmap
once you load the Bitmap use this to scale it upto 900X300
Bitmap createScaledBitmap (Bitmap src, int dstWidth, int dstHeight, boolean filter)
then use this method to make a cut out of it at 300 X 300
Bitmap createBitmap (Bitmap source, int x, int y, int width, int height)

Android Imageview: Measure unspecified to calculate Bitmap dimensions

Google suggests to load Bitmaps from resources scaled down, depending on the actual ImageView size ("Loading Large Bitmaps Efficiently" at googles developer guides). Therefor, I have to know the width and height of the ImageView before I can decode the bitmap.
My code looks something like the one posted below. decodeSampledBitmapFromResources returns the bitmap as a scaled down version of the one stored in resources.
public void onCreate(Bundle savedInstanceSate)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout)
ImageView imageView = (ImageView) findViewById(R.id.myImageView);
/**
At this point, I need to calculate width and height of the ImageView.
**/
Bitmap bitmap = MyBitmapManager.decodeSampledBitmapFromResource(
getResources(), R.drawable.my_icon, width, height);
imageView.setImageBitmap(bitmap);
}
The problem is, as I am in onCreate, my ImageView does not have any width and height, yet. getWidth() and getHeight() are just returning 0. I stumbled over this code to calculate the size of a view before it is actually drawn:
ImageView v = findViewById(R.id.myImageView);
v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
int width = v.getMeasuredWidth();
int height = v.getMeasuredHeight();
Is this suitable for my situation? I tried it and the above code returns some values for width and height which seem to be correct, but Im not sure if this is the correct way to do it.
UPDATE:
After some more testing, this seems NOT to work.
In the above example, im using a PNG with a size of 192x192 pixels.
After measureing the ImageView as seen above, i get a measured dimension of 128x128.
If I call getWidth() and getHeight() AFTER the bitmap is set to the imageview, the dimension is 100x100.
So in this scenario, the image is downsized from 192x192 to 128x128, but not to 100x100 as it should be.
It seems that Measurespec.UNSPECIFIED always returns dimensions greater than they are at the end.
Thanks in advance,
danijoo
I got it solved on my own with this:
ViewTreeObserver vto = imageView.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw(){
// at this point, true width and height are already determined
int width = imageView.getMeasuredWidth();
int height = imageView.getMeasuredHeight();
Bitmap bitmap = MyBitmapManager.decodeSampledBitmapFromResource(
getResources(), R.drawable.my_icon, width, height);
imageView.setImageBitmap(bitmap);
// this is important because onPreDrawn is fired multiple times
imageView.getViewTreeObserver().removeOnPreDrawListener(this);
return true;
}
}

How to get scaled instance of a bufferedImage

I wanted to get scaled instance of a buffered image and I did:
public void analyzePosition(BufferedImage img, int x, int y){
img = (BufferedImage) img.getScaledInstance(getWidth(), getHeight(), Image.SCALE_SMOOTH);
....
}
but I do get an exception:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
at ImagePanel.analyzePosition(ImagePanel.java:43)
I wanted then to cast to ToolkitImage then use the method getBufferedImage I read about in other articles. The problem is there is no class such as sun.awt.image.ToolkitImage I cannot cast to it because Eclipse does not even see this class. I use Java 1.7 and jre1.7.
You can create a new image, a BufferedImage with the TookitImage.
Image toolkitImage = img.getScaledInstance(getWidth(), getHeight(),
Image.SCALE_SMOOTH);
int width = toolkitImage.getWidth(null);
int height = toolkitImage.getHeight(null);
// width and height are of the toolkit image
BufferedImage newImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
Graphics g = newImage.getGraphics();
g.drawImage(toolkitImage, 0, 0, null);
g.dispose();
// now use your new BufferedImage
BufferedImage#getScaledInstance is actually inherited from java.awt.Image and only guarantees that it will return an Image so I would say it's not a good idea to try and assume the underlying return type in this case.
getScaledInstance is, also, not normally the fastest or best quality method
To scale a BufferedImage itself, you have a number of different options, but most simply take the original and repaint it to another image, applying some kind of scaling in process.
For example:
Scale the ImageIcon automatically to label size
Position Image in any Screen Resolution
how to make image stretchable in swing?
For more details about getScaledInstance, have a read of The Perils of Image.getScaledInstance()

Android Java create bitmap from hex value

Can anyone suggest a way to create a small solid colored bitmap image from a hex value?
Alternatively, you can use Bitmap.eraseColor() to set a solid color for your bitmap.
Example:
import android.graphics.Bitmap;
...
Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
image.eraseColor(android.graphics.Color.GREEN);
I think I may have the answer. Technically I believe it is much easier on Android than on a "pc". The last time I searched to create a bitmap (.bmp), I only found some Android functions and the BitmapFactory for non-android, which didn't work for me.
Please look at this site: http://developer.android.com/reference/android/graphics/Bitmap.html
This point could fit for you:
static Bitmap createBitmap(int[] colors, int offset, int stride, int width, int height, Bitmap.Config config)
Returns a immutable bitmap
with the specified width and height, with each pixel value set to the
corresponding value in the colors array.
Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas=new Canvas (image);
int HEX=0xFF888888;
canvas.drawColor (HEX);
Use the createBitmap().
Here is a link that will show you how: http://developer.android.com/reference/android/graphics/Bitmap.html

Categories

Resources