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.
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'm decoding an Qr Code that i'm getting throw the Java JSANE API. when i'm writing the image i got from the JSANE API in a file before reading it with IMAGEIO.read(), the decodong process is working but when i'm using the ilage Object given from the JSANE API directly without writing it in a file before reading, i' getting the following error :
Exception in thread "main" com.google.zxing.NotFoundException
The method i'm using are:
public String decode_QrCode(String filePath, String charset) throws FileNotFoundException, IOException, NotFoundException{
System.out.println("ICI ERREUR");
BinaryBitmap binaryBitmap = new BinaryBitmap(
new HybridBinarizer(
new BufferedImageLuminanceSource(
ImageIO.read(new FileInputStream(filePath))
)
)
);
Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap);
return qrCodeResult.getText();
}
public String decode_QrCode_buffImg(BufferedImage bufferedImage) throws NotFoundException{
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
BinaryBitmap binaryBitmap = new BinaryBitmap(
new HybridBinarizer(
source
)
);
Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap);
return qrCodeResult.getText();
}
I'm getting the image from the JSANE API like this:
Image image = dialog.openDialog();
I'm converting the Image to BufferedImage like this:
public static BufferedImage toBufferedImage(Image image)
{
if (image instanceof BufferedImage)
return (BufferedImage)image;
// This code ensures that all the pixels in the image are loaded
image = new ImageIcon(image).getImage();
// Determine if the image has transparent pixels
boolean hasAlpha = hasAlpha(image);
// Create a buffered image with a format that's compatible with the screen
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
// Determine the type of transparency of the new buffered image
int transparency = Transparency.OPAQUE;
if (hasAlpha == true)
transparency = Transparency.BITMASK;
// Create the buffered image
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
} catch (HeadlessException e) { } //No screen
if (bimage == null) {
// Create a buffered image using the default color model
int type = BufferedImage.TYPE_INT_RGB;
if (hasAlpha == true) {type = BufferedImage.TYPE_INT_ARGB;}
bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
}
// Copy image to buffered image
Graphics g = bimage.createGraphics();
// Paint the image onto the buffered image
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
This the main function of the class:
public static void main(String[] args) throws IOException, cf, NotFoundException {
Frame frame = null;
// TODO Auto-generated method stub
JSaneDialog dialog = new JSaneDialog( JSaneDialog.CP_START_SANED_LOCALHOST,
frame, "JSaneDialog", true, null);
Image image = dialog.openDialog();
BufferedImage buffImg = toBufferedImage(image);
//BufferedImage buff = resize(buffImg, BufferedImage.TYPE_INT_RGB, buffImg.getWidth()/2, buffImg.getHeight()/2, 0.5, 0.5);
ImageIO.write(buffImg, "png", new File("/home/michaelyamsi/Bureau/Mémoires/test/test.png"));
QrCode New_Qr = new QrCode();
System.out.println("QR Code decompressed : " + New_Qr.decode_QrCode("/home/michaelyamsi/Bureau/Mémoires/test/test.png", "UTF-8"));
System.out.println("QR Code decompressed : " + New_Qr.decode_QrCode_buffImg(buffImg));
}
I'have even tried to resize th BufferedImage before using it but when i'm doing that, even the decode from the resultant file is not working.
I working on "mini photoshop" in java. I open jpg file useing this method
public BufferedImage open()
{
fc = new JFileChooser();
int ret = fc.showOpenDialog(null);
if (ret == JFileChooser.APPROVE_OPTION)
{
try {
img=ImageIO.read(fc.getSelectedFile());
img2=ImageIO.read(fc.getSelectedFile());
raster = img.getRaster();
} catch (IOException e) {
e.printStackTrace();
}
}else
img = null;
return img;
}
In this picture I make some change eg. sephia, sobel. But after this I want back to origin picture, I use for this method to do this:
public BufferedImage origin() {
raster=img.getRaster();
return img2;
}
The problem arises when I want again use sepia or any other filter because program remember earlier modification.
For example when i use sepia then back to origin and use sobel, program show me sepia+sobel, should show only sobel.
Should the below line
raster=img.getRaster();
change to
raster=img2.getRaster(); ?
public BufferedImage origin() {
raster=img.getRaster();
return img2;
}
I am making a project for Controlling a PC from any other PC on a Network.
but when i send the image from server to client, it does not changed, just the first image is displayed.
So i am sending the captured images of server to client using GZIP compression.
Here is the Server's Code:
out = new DataOutputStream(s.getOutputStream());
zipout = new GZIPOutputStream(out);
while(true)
{
img = conn.getScreenImg(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
//Here conn is a Object of Robot Class
ImageIO.write(img, "jpeg", zipout);
Thread.sleep(200);
System.out.println("Success");
}
Client Code: displaying the Images sent by Server.
while(true)
{
try
{
img = ImageIO.read(zipin);
Graphics g = this.getGraphics();
g.drawImage(img, 0, 0, this);
Thread.sleep(100);
}
catch (Exception e)
{
e.printStackTrace();
}
}
I need Help with this. The image doesn't change on client.
and i want to know that if it is good to use GZIP here for compression for images to send over network , will it accelerate the process. or should i use some other method.
The way you are displaying the images on the client doesn't seem right to me. It would be a lot better to write a custom JPanel that would override the paintComponent method and that would handle the image rendering. Something like below:
class ImagePanel extends JPanel {
private BufferedImage image = null;
private void setImage(BufferedImage img) {
image = img;
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if (image != null) {
g2.drawImage(image, 0, 0, null);
}
}
}
Inside your while loop you would call the setImage method on the panel. The panel in of course previously instantiated and added to a container.
I am currently trying to resize a picture that I am downloading from the web and putting it into a JPanel.
First, I am using the following code to download the image from the web:
public static Image MSImageHigh(){
URL imageUrl = null;
try {
imageUrl = new URL("http://www.hmdb.ca/labm/metabolites/"
+ HMDB + "/ms/spectraH/" + HMDB + "H.png");
} catch (MalformedURLException e) {
e.printStackTrace();
}
Image image = Toolkit.getDefaultToolkit().createImage(imageUrl);
return image;
}
Then I made a new method that resizes the image:
public static BufferedImage resizeImage() {
final BufferedImage bufferedImage = new BufferedImage(300, 500,BufferedImage.TYPE_INT_RGB);
final Graphics2D graphics2D = bufferedImage.createGraphics();
graphics2D.setComposite(AlphaComposite.Src);
graphics2D.drawImage(MSImageHigh(), 0, 0, 200, 200, null);
graphics2D.dispose();
return bufferedImage;
}
This should produce s a new image that is resized to 200x200 px. What it in fact does is give me a black screen that is 200x200px in size. Btw, I also tried using TYPE_INT_ARGB instead of TYPE_INT_RGB, and this produces a totally transparent image, so that is not working either.
I used ImageIO.read(imageUrl) instead of Toolkit.getDefaultToolkit().createImage(imageUrl) and that solved the problem. Thanks #Hovercraft Full Of Eels!