Java Java: PNGException: Premature End of JPEG File

  • Thread starter Thread starter haxor489
  • Start date Start date
  • Tags Tags
    Java
AI Thread Summary
The discussion revolves around an error encountered while trying to retrieve and process an image from a URL using Java. The error message indicates a "crc corruption" in the PNG image, leading to the image being partially visible and mostly black. The user is attempting to download a map image from Google Maps, and while the URL works fine in a browser, the Java code fails to retrieve the complete image data. The original code uses a BufferedInputStream to read the image data into a byte array based on the content length, but this approach does not ensure that the entire image is read, resulting in corruption. A suggestion is made to simplify the image retrieval process by directly using the Toolkit's createImage method with the URL, which would handle the image loading more effectively. The user ultimately realizes that a more robust reading method is needed to ensure the full content is captured, rather than relying on available data in the stream.
haxor489
Messages
25
Reaction score
0
I'm getting this error

sun.awt.image.PNGImageDecoder$PNGException: crc corruption
at sun.awt.image.PNGImageDecoder.getChunk(Unknown Source)
at sun.awt.image.PNGImageDecoder.getData(Unknown Source)
at sun.awt.image.PNGFilterInputStream.read(Unknown Source)
at java.util.zip.InflaterInputStream.fill(Unknown Source)
at java.util.zip.InflaterInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.awt.image.PNGImageDecoder.produceImage(Unknown Source)
at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
at sun.awt.image.ImageFetcher.run(Unknown Source)
This is the segment of code causing the error.

Code:
URLConnection con = new URL("www.example.com").openConnection();
		InputStream is = con.getInputStream();
		byte bytes[] = new byte[con.getContentLength()];
		
		BufferedInputStream bis = new BufferedInputStream(is);
		bis.read(bytes);
		bis.close();
		con.getInputStream().close();
		
		Toolkit tk = getToolkit();
		
		image.setImage(tk.createImage(bytes));
		File save = new File("picture");
		FileOutputStream fos =  new FileOutputStream(save);
		
		fos.write(bytes);
		fos.close();
		if(tk.prepareImage(image.getImage(), 900, 900, null)){
			System.out.println("True");
		}else
			System.out.println("False");

The file i output to shows that the image is corrupt, only a few lines of the image are visible and the rest is black. tk.prepareImage() also returns false. This segment sits in a method called once in its class's constructor method. I'm contacting the maps.google server for a map image with the proper parameters. The image returns fine with the same url in a browser . I've checked the google maps API documentation but nothing stands out. Any ideas?Edit: Attempted requesting a different image format, but now i get "Premature end of JPEG file"
 
Last edited:
Technology news on Phys.org
haxor489 said:
This is the segment of code causing the error.

Code:
URLConnection con = new URL("www.example.com").openConnection();
		InputStream is = con.getInputStream();
		byte bytes[] = new byte[con.getContentLength()];
		
		BufferedInputStream bis = new BufferedInputStream(is);
		bis.read(bytes);
		bis.close();
		con.getInputStream().close();
		
		Toolkit tk = getToolkit();
		
		image.setImage(tk.createImage(bytes));
		File save = new File("picture");
		FileOutputStream fos =  new FileOutputStream(save);
		
		fos.write(bytes);
		fos.close();
		if(tk.prepareImage(image.getImage(), 900, 900, null)){
			System.out.println("True");
		}else
			System.out.println("False");

The file i output to shows that the image is corrupt, only a few lines of the image are visible and the rest is black. tk.prepareImage() also returns false. This segment sits in a method called once in its class's constructor method. I'm contacting the maps.google server for a map image with the proper parameters. The image returns fine with the same url in a browser . I've checked the google maps API documentation but nothing stands out. Any ideas?Edit: Attempted requesting a different image format, but now i get "Premature end of JPEG file"

This seems like a really convoluted way to grab an image from a URL. If the URL contains nothing but the image, why not just use something like
Code:
try {
    Image image = tk.createImage(new URL("www.example.com"));
} catch (//whatever exceptions URL's constructor & ToolKit.createImage(URL u) throw) {
    //handle exceptions
}
 
Last edited:
I figured it out. Your way is a lot easier, I'm just used to doing things on a much lower level (C programmer). The stream reader would only read what was available() into the array and not the full contentLength() but a while check sum loop read the full content.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top