abnormal resizing of java.awt.Image on using getImage() and getScaledInstance() - java

I m making a java rmi based application where i pass and receive an ImageIcon object from the server...
(the image is stored in a separate URL in server)
The function involves the following....
1. Getting the image from the server at first....(on button press A)
2. Replacing it with a image file in the client[optional]....(on button press B)
3. Remove the image with a default image[optional]....(on button press C)
4. Sending it back to the Server....................(On button press D).....
Here the image is displayed in ajlabel calledimg_label
The codes i've used are as follows.....
Variables used
java.awt.Image img;
javax.swing.ImageIcon CurrentImageIcon;
javax.swing.ImageIcon DefaultImageIcon;
// CurrentImageIcon contains the image to be displayed in the img_label....
// img is used for copying as well for scaling......
// DefaultImageIcon holds the default Image......
On Button Press A
img = temp.getImage();
CurrentImageIcon = new ImageIcon(img);
// Assuming temp holds the ImageIcon taken from the server.......
img=img.getScaledInstance(83,85 , Image.SCALE_DEFAULT);
img_label.setIcon(new ImageIcon(img));
img_label.revalidate();
img_label.repaint();
On Button Press B
String url_text = jTextField.getText(); // taking the url frm the field.....
CurrentImageIcon = new ImageIcon(url_text);
img=CurrentImageIcon.getImage();
img=img.getScaledInstance(83,85 , Image.SCALE_DEFAULT);
img_label.setIcon(new ImageIcon(img));
img_label.revalidate();
img_label.repaint();
On Button Press C
img = DefaultImageIcon.getImage();
CurrentImageIcon = new ImageIcon(img);
img=img.getScaledInstance(83,85 , Image.SCALE_DEFAULT);
img_label.setIcon(new ImageIcon(img));
img_label.revalidate();
img_label.repaint();
On Button Press D
// ImagetoSend is an ImageIcon to be sent to the Server.....
ImagetoSend = CurrentImageIcon;
CurrentImageIcon = null;
Now the problem i m getting is a weird one......
The image is getting embedded as i wanted on repainting when i click this button........
But when i download the recently uploaded image next time on Button press A....it is displayed either magnified or reduced to size even though i included the getScaledInstance method....
like this...
The image i am handling is a jpg image....
I even checked the image at Server directory.....No size change has occured on that file which was uploaded from client to server. But the change is observed when it is downloaded and embedded to the jlabel...
Can Anyone help me to sort out this issue...??

Somehow it is getting scaled down twice. Either you scale it down then send it to the server where it is scaled down, or the both button clicks scale it. Eliminate the scale down code on the server side and see what happens.

Related

Image Capture Specific part of screen

I'm trying to capture a specific part of my screen and save it as a JPG image.
This is what I've tried thus far:
Robot r = new Robot();
// It saves screenshot to desired path
String path = "Health_Bar.JPG";
// Used to get ScreenSize and capture image
//Dimension dim = new Dimension(376, 54, 58, 18);
Rectangle capture =
new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage Image = r.createScreenCapture( new Rectangle(191, 229, 473, 150) );//r.createScreenCapture(capture);
ImageIO.write(Image, "jpg", new File(path));
System.out.println("Screenshot saved");
This code is not working as expected. I expect the x and y coordinate to be the top left corner coordinate of the image captured. And even though this is true in some cases i.e. if x,y=0; it is not in most other cases i.e x=191, y=229, as in the example code above.
For the example code above this is the out I get output image from code. However this is the image I expect to get Image of expected output. The coordinates in image 2 (expected output image) is the coordinate for the top left corner of the "Mofiki's Coordinate Finder" window.
Can anyone explain whats going on and how to fix it?

codename one image from url

I am trying to make a mobile app using Java and codename one plugin. My question is - what's the simples way to populate an image from a URL to a label? I Googled it and all I found was this piece of code:
Image i = URLImage.createToStorage(placeholder, "fileNameInStorage", "http://xxx/myurl.jpg", URLImage.RESIZE_SCALE);
But I have no idea how to use it. What is a placeholder? It asks an EncodedImage parameter, but if I do:
EncodedImage image = new EncodedImage(10, 10);
I get the error that EncodedImage is protected.
I simply want to populate an image from a URL to my desired label in a form.
I am using GUI builder.
The placeholder image is the image that should show while the image from URL is being downloaded and it's an EncodedImage.
If your Label already have an icon as a placeholder, you can use its icon, otherwise you can create a new placeholder image. Below are 3 options to create an EncodedImage and a URLImage usage example:
Method 1:
//generate a grey placeholder that matches the size of the label's icon
Image placeholder = Image.createImage(label.getIcon().getWidth(), label.getIcon().getWidth(), 0xbfc9d2);
EncodedImage encImage = EncodedImage.createFromImage(placeholder, false);
Method 2:
//Convert the label icon to EncodedImage
EncodedImage encImage = (EncodedImage)label.getIcon();
Method 3:
//Create a fresh grey EncodedImage when label doesn't have any icon set initially
int deviceWidth = Display.getInstance().getDisplayWidth();
Image placeholder = Image.createImage(deviceWidth / 10, deviceWidth / 10, 0xbfc9d2); //square image set to 10% of screen width
EncodedImage encImage = EncodedImage.createFromImage(placeholder, false);
Usage Example:
It's a good practice to use the URL as the cached image name in storage. If you have a multiple sizes of the same image, just prefix them with a unique string like "Large" + URL
label.setIcon(URLImage.createToStorage(encImage, "Medium_" + "http://xxx/myurl.jpg", "http://xxx/myurl.jpg", URLImage.RESIZE_SCALE));

java jLabel image does not update

I'm creating a feedback label that displays the picture that the user has chosen in a file dialog.
The moment when a picture file is selected, the label will update itself into that image of which the user has clicked.
The first time when the picture is chosen it works fine, however when another picture is chosen for the 2nd time onwards, it remains as the first picture.
Codes:
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
//browse button
FileDialog fd = new FileDialog(this, "Choose a file", FileDialog.LOAD);
fd.setDirectory("C:\\");
fd.setFile("*.jpg"); // jpg files only
fd.setVisible(true);
String filename = fd.getFile();
if (filename == null) {
System.out.println("You cancelled the choice");
} else {
savePicture("temp"); // save it in temp.jpg. This overwrites any existing picture.
ImageIcon imgThisImg = null;
imgThisImg = new ImageIcon(absfilePath+ "/temp.jpg");
jLabel7.setIcon(null);
jLabel7.setIcon(imgThisImg);
jLabel7.revalidate();
jLabel7.repaint();
}
During debugging, the moment after savePicture() function is executed, the directory picture is updated. Therefore it's not an issue with overwritting the file. The file is overwritten correctly, why does it still display the previous image? Is there a cache or something that i need to clear?
Thanks.
Using ImageIO to the read file works best. Can be achieved using the following line.
jLabel7.setIcon(new JLabel(new ImageIcon(ImageIO.read(new File("C:\\Users\\Cameron Gillespie\\Documents\\NetBeansProjects\\OnlineCabsClient\\src\\images\\taxiBackground.png")))));
You are taking the label then setting the icon. Creating a new label and ImageIcon. Then using ImageIO to read the file. Reading the image and printing it to the label.

Swing - Get URL used by ImageProducer to produce an Image

I am trying to get to the url of an Image.
Icon icon = getIcon(); // gets a previously initialised Icon
ImageIcon imageIcon = (ImageIcon) icon;
Image image = imageIcon.getImage();
image.getSource(); // returns a java.awt.image.FilteredImageSource
The FilteredImageSource has a property src, which contains the url of the image.
However the src property is with default access modifier.
Is there any way I can get the image url at this point in time ?

Java Swing: popup : display small icon in menu

I have a right click popup menu that works fine.
I now want to add a little image next to the text.
I managed to display the image in the popupmenu but its the full size version.
what I want to know is:
is there a way to automatically reize the icon to be the right size ( same as the text ) or do I have to resize the image?
Jason
You have to resize the Icon to be whatever size you want it to be. Something like:
ImageIcon icon = new ImageIcon( ... );
Image image = icon.getImage().getScaledInstance(64, 64, Image.SCALE_SMOOTH);
JLabel label = new JLabel( new ImageIcon( image ) );

Categories