2

I want to set the size of image get through json service. ???

Here is my code

public void getImage(){
 try {

        URL url = new URL("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=good%20idea%20good%20idea%20water%20jug%20water%20jug%20thirsty%20crow%20thirsty%20crow%20long%20time%20long%20time%20jug%20jug");
        URLConnection connection = url.openConnection();
        String line;
        StringBuilder builder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }
        JSONObject json = new JSONObject(builder.toString());
        String imageUrl = json.getJSONObject("responseData").getJSONArray("results").getJSONObject(0).getString("url");

        BufferedImage image = ImageIO.read(new URL(imageUrl));

        ImageIcon img = new ImageIcon(image);
        JLabel label = new JLabel("", img, JLabel.CENTER);

        jPanel1.add( label, BorderLayout.CENTER );


    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Failure", JOptionPane.ERROR_MESSAGE);
        e.printStackTrace();
    }     
}

I am showing the image in a jPanel.I want to set the size of image so it must be smaller than panel size. How could I achieve this ????

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
noman
  • 37
  • 2
  • 14
  • [maintaining aspect ratio of JPanel background image](http://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928) and [Quality of Image after resize very low](http://stackoverflow.com/questions/14115950/quality-of-image-after-resize-very-low-java/14116752#14116752) – MadProgrammer Jun 17 '15 at 05:17
  • You have a catch 22 problem, the size of the panel is normally determined by the size of the contents (the image in this case), maybe something like [this](http://stackoverflow.com/questions/18053310/how-to-make-image-stretchable-in-swing/18054307#18054307) might help. It's a `JPanel` which can paint the image, but which scales the image so that it will "fit" within it – MadProgrammer Jun 17 '15 at 05:21
  • I have tried the code [this] I don't need the image to be stretchable so i **updated / altered** my code according to my need but could not find this helpful. This is a good work but i don't find this helpful. So is there any **method** that altered size of images by directly **specifying width, height** to json image. – noman Jun 17 '15 at 05:57
  • `BufferedImage#getScaledInstance` - but then I'd have to point you to [The Perils of Image.getScaledInstance()](https://today.java.net/article/2007/03/30/perils-imagegetscaledinstance). You could also have a look at [imgscalr](http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/), but as I said, you may have a catch 22 problem, in the fact that the size of the panel is normally dictated by the size of it's content (the `JLabel` and image). The first link I posted also scales an image while maintaining it's aspect ratio – MadProgrammer Jun 17 '15 at 06:07

2 Answers2

2

If you don't care about the quality of the result, you could use Image#getScaledInstance, but you should have a read of The Perils of Image.getScaledInstance() of first

Image scaled = image.getScaledInstance(jPanel1.getWidth(), jPanel1.getHeight(), Image.SCALE_SMOOTH);

But this won't maintain the aspect ratio of the image, to do that, you'd have to decide which dimension is more important

int width = jPanel1.getWidth();
int height = jPanel1.getHeight();
if (width < height) {
    height = -1;
} else {
    width = -1;
}
Image scaled = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);

Have a look at Java: maintaining aspect ratio of JPanel background image for more details

Now, if you do care about the quality of the resulting image, then you need to use a different scaling approach, you could use something like a divide and conqure approach, as demonstrated in Quality of Image after resize very low -- Java or use a 3rd party library, like ImgSclr

Now, you next problem is, the size of a component is generally calculated based on the needs of it's children. In this case, that means that jPanel1 may want to use the size of the JLabel to determine how large it should be. Equally, the JLabel will use the size of the image to determine how large it should be. Which puts you in a catch 22 problem.

You could use a JScrollPane which will allow the image to occupy a large area then is available or you will need a dynamic panel which can resize the image automatically based on the available space, but you would still need to make a determination of what the "default" or "preferred" size should be

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

try this:

resized = Bitmap.createScaledBitmap(yourBitmap,(int)(yourBitmap.getWidth()*0.8), (int)(yourBitmap.getHeight()*0.8), true);
  • It's that an Android API? – MadProgrammer Jun 17 '15 at 06:05
  • `ImageIcon img = new ImageIcon(image)` I get image from JSON it is of type ImageIcon. The above code image is of type **Bitmap** not working – noman Jun 17 '15 at 06:07
  • No Its not an Android API. It is Google API AJAX Service. It will call Image results in JSON fromat .Check out This . Provide image url in JSON Data /URL body – noman Jun 17 '15 at 06:14
  • `BitMap` is not a standard class of the Java API, is part of a Google library? I've only seen it in the Android API – MadProgrammer Jun 17 '15 at 06:27
  • ok. coming back to my question. I have get image from google ajax api in JSON format How do I set the width and height of that image? – noman Jun 17 '15 at 06:44