I am implementing a screenshot capture in my Java web Application Which Is Already Working. I however have an issue with the screenshot because the Image is Taken for the entire Screen:
As you can see this is the entire screen, I just want to capture the area with the graphs(the White area). The Code I am using to capture this screen is:
public void captureGraphs() {
try {
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(new Rectangle(size));
File f = new File("C:/capture");
if (!f.exists()) {
f.mkdir();
}
File[] flist = f.listFiles();
for (File flist1 : flist) {
flist1.delete();
}
ImageIO.write(img, "JPG", new File("C:/capture/screenShot.jpg"));
System.out.println("Capture Successfull");
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_INFO,
"Successful!",
"You have successfully taken a snapshot. Thank You"));
} catch (HeadlessException e) {
System.out.println(e.getMessage());
} catch (AWTException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
Is there any way I can change this code to capture the Mid Part only holding the graphs??
Related
I'm trying to download an image with exif data from URL to my pc, the problem is that exif data is gone after the image has been downloaded.
here's the code i use
public static void downloadImage(String str) {
try {
URL url = new URL("https://upload.wikimedia.org/wikipedia/commons/6/68/Goldmantelziesel.jpg");
BufferedImage img = ImageIO.read(url);
File file = new File("F:\\Photos\\ww123\\"+str+".jpg");
ImageIO.write(img, "jpg", file);
} catch (MalformedURLException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
I am trying to set my program (java using netbeans) to do one thing if it finds the image its looking for, and another thing if it doesn't...
this is what I have got so far however it never completes the else statement. I believe this is because "image" is not technically null, as it still has corresponds to the filename entered, however I am not sure how to set java to do something based on if the filename is not found within the directory.
public void displayImage(String strfilename, JLabel JLlabel) {
try {
JLabel label = JLlabel;
String FileName = strfilename;
BufferedImage image = ImageIO.read(new File(FileName + ".jpg"));
if(image!=null){
ImageIcon icon = new ImageIcon(image);
label.setIcon(icon);}
else{
BufferedImage image2 = ImageIO.read(new File("NOIMAGE.jpg"));
ImageIcon icon2 = new ImageIcon(image2);
label.setIcon(icon2);
}
} catch (IOException ioe) {
}
}
If anyone could help me with this I would be very grateful
You're swallowing the exception. You don't need an if{}else{} loop since you already have a try{}catch{}.
String FileName = file;
try {
BufferedImage image = ImageIO.read(new File(FileName + ".jpg"));
// Code for when the image is found
} catch (IOException ex) {
// Code for when the image is not found
}
EDIT:
As #haraldK pointed out, you can have a file that exists but is unreadable, in which case a NullPointerException will be thrown.
You can handle them both in the catch clause.
public void displayImage(String strfilename, JLabel label) {
try {
BufferedImage image = ImageIO.read(new File(strfilename + ".jpg"));
ImageIcon icon = new ImageIcon(image); // Can throw NullPointerException if the file is found but is unreadable
label.setIcon(icon);
} catch (IOException | NullPointerException ex) {
ImageIcon icon = new ImageIcon("NOIMAGE.jpg");
label.setIcon(icon);
}
// You're probably going to have to pack or validate your container here
}
One thing worth noting is that this does not check exceptions for the NOIMAGE, you might want to add that.
This is better than just calling File.exists() as it also handles files that exist but are unreadable (text file and whatnot).
you can check the file if exist before you process this method also i removed some unnecessary code.
public void displayImage(String strfilename, JLabel JLlabel) {
// declare only one reference you don't need two references
BufferedImage image=null;
if(!isImageExist(strfilename)){
// assign the NOIMAGE if image not found
image = ImageIO.read(new File("NOIMAGE.jpg"));
} else {
try {
// assign the image if found
image = ImageIO.read(new File(strfilename + ".jpg"));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
ImageIcon icon = new ImageIcon(image);
//setting the image once instead of repeating the code in if and else blocks also you don't need to add another reference to JLlabel because you are already got it from a parameter
JLlabel.setIcon(icon);
}
private boolean isImageExist(String imageName) {
return new File(imageName.jpg).exist();
}
I'm trying to take a screenshot of what's behind of a JPanel but i get a black image after saving it.
Here's the code:
private void takeScreenshot(String print){
JFileChooser c = new JFileChooser(getRealDesktop());
FileFilter jpg = new FileNameExtensionFilter(".jpg", ImageIO.getReaderFileSuffixes());
FileFilter jpeg = new FileNameExtensionFilter(".jpeg", ImageIO.getReaderFileSuffixes());
FileFilter png = new FileNameExtensionFilter(".png", ImageIO.getReaderFileSuffixes());
c.setFileFilter(png);
c.setFileFilter(jpeg);
c.setFileFilter(jpg);
c.showSaveDialog(this);
if(c.getSelectedFile() != null){
String ssLoc = c.getSelectedFile().getAbsolutePath()+c.getFileFilter().getDescription();
System.out.println(ssLoc);
BufferedImage bufImg = new BufferedImage(ssWindow.getSize().width, ssWindow.getSize().height,BufferedImage.TYPE_INT_RGB);
ssWindow.paint(bufImg.createGraphics());
File imageFile = new File(ssLoc);
try{
imageFile.createNewFile();
ImageIO.write(bufImg, "JPG", imageFile);
}catch(Exception ex){
System.err.println(ex);
}
}
}
And the press action button looks like this:
this.setVisible(false);
takeScreenshot("ssTake");
this.setVisible(true);
How can make this work?
Thanks, Gilbert Le Blanc.
I reworked a bit the code to use the robot for this and now the code looks like this:
private void takeScreenshot(String print){
JFileChooser c = new JFileChooser(getRealDesktop());
FileFilter jpeg = new FileNameExtensionFilter("JPEG (*.jpg;*.jpeg;*.jpe;*.jfif)", "jpg", "jpeg", "jpe", "jfif");
c.setFileFilter(jpeg);
c.showSaveDialog(this);
if(c.getSelectedFile() != null){
ssLoc = c.getSelectedFile().getAbsolutePath()+".jpg";
try {
Thread.sleep(150);
Robot robot = new Robot();
BufferedImage screenShot = robot.createScreenCapture(new Rectangle(ssWindow.getX(),ssWindow.getY(),ssWindow.getWidth(),ssWindow.getHeight()));
ImageIO.write(screenShot, "JPG", new File(ssLoc));
System.out.println(print);
} catch (AWTException | IOException | InterruptedException ex) {
Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
I've got what seems like a very simple section of code, and I can't work out for the life of me why it's not working.
I have a method that listens for image updates from a camera and when it recieves them it calls another segment of code.
My listener is:
public void imageUpdated(BufferedImage image) {
if (null != video) {
video.setImage(image);
}
File outputfile = new File("savedingui.jpg");
try {
ImageIO.write(image, "jpg", outputfile);
} catch (IOException e) {
e.printStackTrace();
}
Which happily saves the correct image to disc. However when I save the image again from the setImage method (called on line 3 of the listener code)
public void setImage(BufferedImage image) {
File outputfile = new File("savedorig.jpg");
try {
ImageIO.write(image, "jpg", outputfile);
} catch (IOException e) {
e.printStackTrace();
}
It now just saves a jpeg of black. But the right sized square of black.
Any clues as to whats going on?
I can not reproduce your issue with the following source (which is basically copied from your question):
public static void imageUpdated(BufferedImage image) {
setImage(image);
File outputfile = new File("savedingui.jpg");
try {
ImageIO.write(image, "jpg", outputfile);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void setImage(BufferedImage image) {
File outputfile = new File("savedorig.jpg");
try {
ImageIO.write(image, "jpg", outputfile);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
BufferedImage image = ImageIO.read(new File("test.jpg"));
imageUpdated(image);
}
Is the same instance used somewhere else, e.g. camera writing updated data in it?
I have the code to capture image from screen in java, I have the final captured image as BufferedImage object and Can cast it to ImageIcon
The problem is when sending that file to android can't read it as bitmap drawable. Any one have answer to this ?
Code to send (Java)
BufferedImage image = robot.createScreenCapture(rectangle);
ImageIcon imageIcon = new ImageIcon(image);
//Send captured screen to the server
try {
System.out.println("before sending image");
oos.writeObject(imageIcon);
oos.reset(); //Clear ObjectOutputStream cache
System.out.println("New screenshot sent");
} catch (IOException ex) {
ex.printStackTrace();
}
Android Receiver Part
Thread t= new Thread(new Runnable() {
#Override
public void run() {
while (true) {
try {
client= sc.accept();
is = client.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BitmapDrawable imageIcon = null;
try {
ois = new ObjectInputStream(is);
imageIcon = (BitmapDrawable) ois.readObject();
//Drawable d = Drawable.createFromStream(is, null);
IV.setImageDrawable(imageIcon);
} catch (OptionalDataException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("New image recieved");
}
}
I get the exception of it can't cast the imageIcon or the BufferedImage to Bitmap drawable.
You should not use objects, because oracle jvm is not the same as android's dalvik.
Try to convert captured image to raw bytes and send those bytes to android. Then in android covert this raw data to drawable.