Okay, so I've been trying to load a BufferedImage using this code:
URL url = this.getClass().getResource("test.png");
BufferedImage img = (BufferedImage) Toolkit.getDefaultToolkit().getImage(url);
This gives me a type cast error when I run it though, so how do I properly load a BufferedImage?
Use ImageIO.read() instead:
BufferedImage img = ImageIO.read(url);
BufferedImage img = null;
try {
img = ImageIO.read(new File("D:\\work\\files\\logo.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Related
How can I enhance the sharpness of an image that has been scaled down from 100px to 25px using an image scaler, as it appears to be heavily pixelated?
The rescale code I am using:
public ImageIcon scaleImage(String location, int size){
BufferedImage img = null;
try {
img = ImageIO.read(new File(location));
} catch (IOException e) {
e.printStackTrace();
}
Image dimg = img.getScaledInstance(size, size, Image.SCALE_SMOOTH);
ImageIcon p = new ImageIcon(dimg);
return p;
}
Using a non-resized picture makes the navigation bar larger and does not fit the label.
Pictures scaled down using other applications look the same.
I created a plot on Canvas. Now I want to add the image to an Excel file.
I know how to get a WritableImage from Canvas and I know that I need an InputStream to write an image in Excel with addPicture(). The problem is how to link these two.
I could save this image to file and then open and load it to Excel, but maybe there is a way to avoid this?
You could use a PipedInputStream/PipedOutputStream to accomplish this:
Image image = ...
BufferedImage bImage = SwingFXUtils.fromFXImage(image, null);
PipedOutputStream pos;
try (PipedInputStream pis = new PipedInputStream()) {
pos = new PipedOutputStream(pis);
new Thread(() -> {
try {
ImageIO.write(bImage, "png", pos);
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
}).start();
workbook.addPicture(pis, Workbook.PICTURE_TYPE_PNG);
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
or you could write the data to a array using ByteArrayOutputStream:
Image image = ...
BufferedImage bImage = SwingFXUtils.fromFXImage(image, null);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ImageIO.write(bImage, "png", bos);
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
workbook.addPicture(bos.toByteArray(), Workbook.PICTURE_TYPE_PNG);
I'm looking to save an image from a website URL that ends in a .jpg extension. Here is the code I'm using:
private void saveImage(String imageURL){
Image img = null;
imageURL = imageURL.substring(2);
try {
URL url = new URL(imageURL);
img = ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
}
BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
// Draw the image on to the buffered image
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
try {
ImageIO.write(bimage, "jpg", new File(System.getProperty("user.home") + "/Desktop/image.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
An example URL is this: i.imgur.com/fq4ZpIEb.jpg.
But, everytime I run this code it returns this error message:
java.net.MalformedURLException: no protocol: i.imgur.com/fq4ZpIEb.jpg
Does anyone know a solution? Thanks!
java.net.MalformedURLException: no protocol: i.imgur.com/fq4ZpIEb.jpg
You need the "http://"
ie: "http://i.imgur.com/fq4ZpIEb.jpg" would be the URL.
You have to specify the protocol say http, https or ftp in the image URL
I am trying to convert BufferedImage into bitmap to set bitmap in ImageView but I am getting an error. Is there any way to set java.awt.Image in ImageView?
BufferedImage bImageFromConvert = null;
InputStream in = new ByteArrayInputStream(byt);
try {
bImageFromConvert = ImageIO.read(in);
Image ima = bImageFromConvert;
Bitmap bmp = Bitmap.;
img.setImageBitmap(bmp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Is there any way to set java.awt.Image in ImageView?
Short answer: No.
You can't use Image, BufferedImage (java.awt package) or ImageIO (javax.imageio package) from Android. So I'm guessing the error you see is related to that.
Maybe if you try to ask how to achieve your goal, rather than this specific implementation issue, maybe we could help you further. :-)
I got a BufferedImage and want to rescale it before saving it as an jpg/png.
I got the following code:
private BufferedImage rescaleTo(BufferedImage img,int minWidth,int minHeight) {
BufferedImage buf = toBufferedImage(img.getScaledInstance(minWidth, minHeight, Image.SCALE_DEFAULT));
BufferedImage ret = new BufferedImage(buf.getWidth(null),buf.getHeight(null),BufferedImage.TYPE_INT_ARGB);
return ret;
}
public BufferedImage toBufferedImage(Image img) {
BufferedImage ret = new BufferedImage(img.getWidth(null),img.getHeight(null),BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = ret.createGraphics();
g2.drawImage(img,0,0,null);
return ret;
}
public String saveTo(BufferedImage image,String URI) throws UtilityException {
try {
if(image == null)
System.out.println("dododod");
ImageIO.write(image, _type, new File(URI));
} catch (IOException e) {
throw new UtilityException(e.getLocalizedMessage());
}
return URI;
}
But as an result I just get a black picture. It must have to do with the rescaling as when I skip it I can save the expected picture.
As a test, set _type="png" and also use file extension .png when you make the call to ImageIO.write(image, _type, new File(URI));. I had issues like you describe and I started writing type PNG and all works fine. Unfortunately, I never went back to debug why I could not write type JPG, GIF etc.