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 need to save a captured image from camera on a tmp file and retrieve it on the next activity, so i can handle better the OOM error.
So i did something like this to write the file:
public void savePicAtFile(Bitmap pic){
FileOutputStream out = null;
try {
out = new FileOutputStream("tmpCameraPic");
pic.compress(Bitmap.CompressFormat.JPEG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
now i don't know how to retrieve the compressed image, should i just create a bitmap and getall the stream?
Any help?
I have to copy the chosen image to the application folder. The image will be chosen by clicking the button. Here is the code (ActionListener) for that Button
imageButton.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent arg0) {
String outPath = new java.io.File("").getAbsolutePath();
fileChooser.showOpenDialog(null);
File pic = fileChooser.getSelectedFile();
String inPath = pic.getPath();
try {
Utils.copyFile(inPath, outPath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
The method copyFile is this:
public static void copyFile(String inPath, String outPath) throws IOException
{
FileInputStream fis=new FileInputStream(new File(inPath));
FileOutputStream fos=new FileOutputStream(new File(outPath));
int c;
while((c=fis.read())!=-1)
{
fos.write(c);
}
}
It gives "File not Found Exception" in Blue and in red color it shows "Access is Denied" when i select the image. I am not sure where the problem is there in my code.
Very simply, I could not draw this image.
public class RenderMap extends JPanel {
static BufferedImage brick;
static BufferedImage groundb;
public static void main(String[] args) {
JFrame window = new JFrame("Super Mario");
RenderMap content = new RenderMap();
window.setContentPane(content);
window.setBackground(Color.WHITE);
window.setSize(1200, 800);
window.setLocation(100,0);
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setResizable(false);
window.setVisible(true);
try {
brick = ImageIO.read(new File("SuperMario/brick.png"));
} catch (IOException e) {
}
try {
URL url = new URL("SuperMario/brick.png");
brick = ImageIO.read(url);
} catch (IOException e) {
}
try {
groundb = ImageIO.read(new File("SuperMario/ground.png"));
} catch (IOException e) {
}
try {
URL url = new URL("SuperMario/ground.png");
groundb = ImageIO.read(url);
} catch (IOException e) {
}
}
public Ground ground;
public RenderMap() {}
public void paintComponent(Graphics g) {
if(ground == null) {
ground = new Ground();
}
ground.draw(g);
}
public class Ground implements ImageObserver {
Ground(){}
void draw(Graphics g) {
g.drawImage(groundb, 0, 0, 1200, 800, this);
g.fillOval( 8, 8, 16, 16);
}
#Override
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
// TODO Auto-generated method stub
return false;
}
}
}
This draws the oval, but not the image. I tried changing to another image(that is functioning in another program) and it still won't work, so I know it's not the image's problem.
As suspected, the images are not being loaded properly.
java.net.MalformedURLException: no protocol: SuperMario/brick.png
at java.net.URL.<init>(URL.java:586)
at java.net.URL.<init>(URL.java:483)
at java.net.URL.<init>(URL.java:432)
at RenderMap.main(RenderMap.java:40)
java.net.MalformedURLException: no protocol: SuperMario/ground.png
at java.net.URL.<init>(URL.java:586)
at java.net.URL.<init>(URL.java:483)
at java.net.URL.<init>(URL.java:432)
at RenderMap.main(RenderMap.java:51)
It's failing at the load-by-URL function calls because you passed in an invalid URL. Correct URL format should be like how you access them from a web browser, like: http://someplace.com/SuperMarioFolder/brick.png
To load the images, choose only one way to read them. Either by:
URL - Remove the File blocks and make the file accessible via full URL.
try {
URL url = new URL("http://www.proper-url.com/SuperMario/ground.png");
groundb = ImageIO.read(url);
} catch (IOException e) { }
File - Remove the URL blocks. This will only allow the program to access local files.
try {
groundb = ImageIO.read(new File("SuperMario/ground.png"));
} catch (IOException e) {
}
For your project's purpose, I believe option#2 would suffice.
Moving forward, you may want to use an IDE's debugging feature to go through code execution step-by-step. If you're not using one, you may want to IntelliJ and Eclipse.
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??