Read Tiff format in Java eclipse - java

How do i read a TIFF image using Java IMAGEIO library??(I am using Eclipse Luna)..And once i download the plugin(JAR files) how to give the Classpath so that it can read my input TIFF image file?

Here a quick example to convert a TIFF image into a PNG image.
// quick conversion example
File inputFile = new File("image.tiff");
File outputFile = new File("output.png");
BufferedImage image = ImageIO.read(inputFile);
ImageIO.write(image, "png", outputFile);
Print a list of all supported formats of the JAI ImageIO library.
import javax.imageio.ImageIO;
...
for (String format : ImageIO.getWriterFormatNames()) {
System.out.println("format = " + format);
}
note For the convertion of image formats which have no built-in support a supporting library must be in the classpath. To find the supported formats check https://docs.oracle.com/javase/tutorial/2d/images/loadimage.html or the snippet above.
e.g. for TIFF you could use the jai_imageio-1.1.jar (or newer).
javac -cp jai_imageio-1.1.jar:. Main.java
java -cp jai_imageio-1.1.jar:. Main
If no TIFF format supporting library is in the classpath the above convertion snippet fails with java.lang.IllegalArgumentException: image == null!.
Following formats have built-in support (Java 8)
BMP
GIF
JPEG
PNG
WBMP
jai_imageio-1.1.jar adds support for
JPEG2000
PNM
RAW
TIFF
edit As times goes by and Java 9 is released, a small update, because Java 9 supports TIFF now out of the box.
compile and run with Java 9 without an additional library
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
class TiffToPng {
public static void main(String[] args) throws Exception {
File inputFile = new File("image.tiff");
File outputFile = new File("output.png");
BufferedImage image = ImageIO.read(inputFile);
ImageIO.write(image, "png", outputFile);
}
}
to find supported ImageReader / ImageWriter formats resp. MIME types you could use following snippets
for (String format : ImageIO.getReaderFormatNames()) {
System.out.println("format = " + format);
}
...
for (String format : ImageIO.getReaderMIMETypes()) {
System.out.println("format = " + format);
}
for (String format : ImageIO.getWriterFormatNames()) {
System.out.println("format = " + format);
}
...
for (String format : ImageIO.getWriterMIMETypes()) {
System.out.println("format = " + format);
}

If you are getting the error:
java.lang.IllegalArgumentException: image == null!
Just put the below jar :-
If you are using eclipse, just add it to referenced libraries.
If you are using just simple java file and running it through console, just paste this jar in the class path.
jai_imageio-1.1.jar |
http://www.java2s.com/Code/JarDownload/jai/jai_imageio-1.1.jar.zip
And Import the following :
import com.sun.media.imageio.plugins.tiff.*;

Related

Java 7zip compression is too big

I have a Java program which searches for a folder with the date of yesterday and compresses it to a 7zip file and deletes it at the end. Now I have noticed that the generated 7zip archive files by my program are way too big. When I use a program like 7-Zip File Manager to compress my files it generates an archive which is 5 kb big while my program generates an archive which is 737 kb big for the same files (which have a 873 kb size). Now I am afraid that my program does not compress it to a 7zip file but do a usual zip file. Is there a way to change something in my code so that it generates a smaller 7zip file like 7-Zip File Manager would do it?
package SevenZip;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;
public class SevenZipUtils {
public static void main(String[] args) throws InterruptedException, IOException {
String sourceFolder = "C:/Users/Ferid/Documents/Dates/";
String outputZipFile = "/Users/Ferid/Documents/Dates";
int sleepTime = 0;
compress(sleepTime, outputZipFile, sourceFolder);
}
public static boolean deleteDirectory(File directory, int sleepTime) throws InterruptedException {
if (directory.exists()) {
File[] files = directory.listFiles();
if (null != files) {
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
deleteDirectory(files[i], sleepTime);
System.out.println("Folder deleted: " + files[i]);
} else {
files[i].delete();
System.out.println("File deleted: " + files[i]);
}
}
}
}
TimeUnit.SECONDS.sleep(sleepTime);
return (directory.delete());
}
public static void compress(int sleepTime, String outputZipFile, String sourceFolder)
throws IOException, InterruptedException {
// finds folder of yesterdays date
final Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1); // date of yesterday
String timeStamp = new SimpleDateFormat("yyyyMMdd").format(cal.getTime()); // format the date
System.out.println("Yesterday was " + timeStamp);
if (sourceFolder.endsWith("/")) { // add yesterday folder to sourcefolder path
sourceFolder = sourceFolder + timeStamp;
} else {
sourceFolder = sourceFolder + "/" + timeStamp;
}
if (outputZipFile.endsWith("/")) { // add yesterday folder name to outputZipFile path
outputZipFile = outputZipFile + " " + timeStamp + ".7z";
} else {
outputZipFile = outputZipFile + "/" + timeStamp + ".7z";
}
File file = new File(sourceFolder);
if (file.exists()) {
try (SevenZOutputFile out = new SevenZOutputFile(new File(outputZipFile))) {
addToArchiveCompression(out, file, ".");
System.out.println("Files sucessfully compressed");
deleteDirectory(new File(sourceFolder), sleepTime);
}
} else {
System.out.println("Folder does not exist");
}
}
private static void addToArchiveCompression(SevenZOutputFile out, File file, String dir) throws IOException {
String name = dir + File.separator + file.getName();
if (file.isFile()) {
SevenZArchiveEntry entry = out.createArchiveEntry(file, name);
out.putArchiveEntry(entry);
FileInputStream in = new FileInputStream(file);
byte[] b = new byte[1024];
int count = 0;
while ((count = in.read(b)) > 0) {
out.write(b, 0, count);
}
out.closeArchiveEntry();
in.close();
System.out.println("File added: " + file.getName());
} else if (file.isDirectory()) {
File[] children = file.listFiles();
if (children != null) {
for (File child : children) {
addToArchiveCompression(out, child, name);
}
}
System.out.println("Directory added: " + file.getName());
} else {
System.out.println(file.getName() + " is not supported");
}
}
}
I am using the Apache Commons Compress library
EDIT: Here is a link where I have some of the Apache Commons Compress code from.
Commons Compress is starting a new block in the container file for each archive entry. Note the block counter here:
Not quite the answer you were hoping for, but the docs say it doesn't support "solid compression" - writing several files to a single block. See paragraph 5 in the docs here.
A quick look around found a few other Java libraries that support LZMA compression, but I couldn't spot one that could do so within the parent container file format for 7-Zip. Perhaps someone else knows of an alternative...
It sounds like a normal zip file format (e.g. via ZipOutputStream) is not an option?
Use 7-Zip file archiver instead, it compresses 832 KB file to 26.0 KB easily:
Get its Jar and SDK.
Choose LZMA Compression .java related files.
Add Run arguments to project properties: e "D:\\2017ASP.pdf" "D:\\2017ASP.7z", e stands for encode, "input path" "output path".
Run the project [LzmaAlone.java].
Results
Case1 (.pdf file ):
From 33,969 KB to 24,645 KB.
Case2 (.docx file ):
From 832 KB to 26.0 KB.
I don't have enough rep to comment anymore so here are my thoughts:
I don't see where you set the compression ratio so it could be that SevenZOutputFile uses no (or very low) compression. As #CristiFati said, the difference in compression is odd, especially for text files
As noted by #df778899, there is no support for solid compression, which is how the best compression ratio is achieved, so you won't be able to do as well as the 7z command line
That said, if zip really isn't an option, your last resort could be to call the proper command line directly within your program.
If pure 7z is not mandatory, another option would be to use a "tgz"-like format to emulate solid compression: first compress all files to a non-compressed file (e.g. tar format, or zip file with no compression), then compress that single file in zip mode with standard Java Deflate algorithm. Of course that will be viable only if that format is recognized by further processes using it.

Converting images from .bmp to jpeg2000 on java and eclipse error

I have seen some examples here and installed Java Advanced Imaging Image I/O Tools on my computer, because obviously it is a requirement of processing JPEG2000 images.
After install this i am able to import libraries
e.g.
import com.sun.media.imageio.plugins.*;
after importing, i should be able to use constructors or methods of that library but i am getting this error:
"Access restriction: The type 'J2KImageWriteParam' is not API (restriction on required library 'C:\Program Files (x86)\Java\jre1.8.0_77\lib\ext\jai_imageio.jar')"
After a litle bit research, i found out that i can change eclipse preferences and ignore that error.
I went through this way: Preferences -> Java -> Compiler -> Errors / Warnings -> Deprecated and Restricted API. Then i changed errors to warnings. But now i can not use that library efficient, cause eclipse suggest me nothing about that library.
My first question is; if there is a better way to do that? Or maybe another way to use this library efficient in eclipse?
EDIT: I found out it was a complication of 32 and 64 bit versions. After installing 32bit JDK and reference the jai_imageio.jar it worked fine.
And second; Can anybody give a plain example to me about converting .bmp image to jpeg2000 image. That would help a lot to me about undesrtanding the context.
Thank you
You need to have these in your imports:
import javax.imageio.*;
import javax.imageio.stream.*;
import com.sun.media.imageio.plugins.jpeg2000.*;
import com.sun.media.imageio.stream.*;
and these jars
jai_imageio.jar;jai_codec.jar;jai_core.jar
This is an example that runs fine for me - but dont know if the produced j2k are valid or anything - use your j2000 viewer to check that.
public void toJ2000(String inputFile, String outputFile) throws IOException {
J2KImageWriteParam iwp = new J2KImageWriteParam();
FileInputStream fis = new FileInputStream(new File(inputFile));
BufferedImage image = ImageIO.read(fis);
fis.close();
if (image == null)
{
System.out.println("If no registered ImageReader claims to be able to read the resulting stream");
}
Iterator writers = ImageIO.getImageWritersByFormatName("JPEG2000");
String name = null;
ImageWriter writer = null;
while (name != "com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageWriter") {
writer = (ImageWriter) writers.next();
name = writer.getClass().getName();
System.out.println(name);
}
File f = new File(outputFile);
long s = System.currentTimeMillis();
ImageOutputStream ios = ImageIO.createImageOutputStream(f);
writer.setOutput(ios);
J2KImageWriteParam param = (J2KImageWriteParam) writer.getDefaultWriteParam();
IIOImage ioimage = new IIOImage(image, null, null);
writer.write(null, ioimage, param);
System.out.println(System.currentTimeMillis() - s);
writer.dispose();
ios.flush();
ios.close();
image.flush();
}
public static void main(String[] args) {
TR t=new TR();
try {
t.toJ2000("yel.png", "yel.j2k");
}
catch (Exception ex) { ex.printStackTrace(); }
}

Exporting content to pdf without using iText

I have worked on exporting the html(table)contents to excel using table id. I have used content type like
response.getWriter().write(datatoexport);
response.setHeader("Content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=test_file.xls");
response.getWriter().flush();
response.getWriter().close();
Here, datatoexport is the table id.
It is working fine with excel.
But, if I use content type as pdf like
response.setHeader("Content-Type", "application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=test_file.pdf");
But, I am getting pdf file is corrupted. Any help?
Without using iText or other jars, How can I achieve it? Especially in IE 8
Before sending pdf file to output, you need to generate it on the server side.
To convert your file to PDF I recommend to use OpenOffice in headless mode and JODConverter.
To run OpenOffice in headless mode (in Windows) run the command (assume you have OpenOfficePortable, installed in C:\Apps:
"C:\Apps\OpenOfficePortable\OpenOfficePortable.exe" -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
As you have OpenOffice started in headless mode, run a simple working prototype using JODConverter library:
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import java.io.File;
import java.net.ConnectException;
public class JODConv {
public static void main(String[] args) throws ConnectException {
if (args.length!=2) {
System.out.println("Usage:\nJODConv <file-to-convert> <pdf-file>");
System.exit(0);
}
String sourceFilePath = args[0];
String destFilePath = args[1];
File inputFile = new File(sourceFilePath);
File outputFile = new File(destFilePath);
// connect to an OpenOffice.org instance running on port 8100
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
connection.connect();
// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
// close the connection
connection.disconnect();
}
}

How do I know if a File type is PDF?

This answer How can I determine if a file is a PDF file? recommends to download another library, but my requirement is that I just need to check if a file is directory is of type PDF or not
Using complete library for this use looks like overkill
Are there any ways to know that a Java File is of type PDF?
Well, according to wikipedia PDF files start with magic numbers: "%PDF" (hex 25 50 44 46) so maybe you should check the InputStream from the file and check that.
SimpleMagic is a Java library for resolving content types:
<!-- pom.xml -->
<dependency>
<groupId>com.j256.simplemagic</groupId>
<artifactId>simplemagic</artifactId>
<version>1.8</version>
</dependency>
import com.j256.simplemagic.ContentInfo;
import com.j256.simplemagic.ContentInfoUtil;
import com.j256.simplemagic.ContentType;
// ...
public class SimpleMagicSmokeTest {
private final static Logger log = LoggerFactory.getLogger(SimpleMagicSmokeTest.class);
#Test
public void smokeTestSimpleMagic() throws IOException {
ContentInfoUtil util = new ContentInfoUtil();
File possiblePdfFile = new File("/path/to/possiblePdfFile.pdf");
ContentInfo info = util.findMatch(possiblePdfFile);
log.info( info.toString() );
assertEquals( ContentType.PDF, info.getContentType() );
}
Well, kind of a hackish solution would be to look at the full file name and see if it ends in ".pdf". The following should help:
import javax.activation.*;
public class ShowMimeType
{
public static void main(String[] args) {
FileDataSource ds = new FileDataSource(args[0]);
String contentType = ds.getContentType();
System.out.println("The MIME type of the file " + args[0] + " is: " + contentType);
}
}
If checking the file extension is not satisfactory, you coudl try checking the files magic number by reading a few bytes of the file
PDF files start with "%PDF" (hex 25 50 44 46).
Combines lighter URLCOnnection.guessContentTypeFromStream() which returns null for some mimeTypes, with heavier AutoDetectParser.
if(currentImageType ==null){
ByteArrayInputStream is = new ByteArrayInputStream(image);
String mimeType = URLConnection.guessContentTypeFromStream(is);
if(mimeType == null){
AutoDetectParser parser = new AutoDetectParser();
Detector detector = parser.getDetector();
Metadata md = new Metadata();
mimeType = detector.detect(is,md).toString();
if (mimeType.contains("pdf")){
mimeType ="pdf";
}
else if(mimeType.contains("tif")||mimeType.contains("tiff")){
mimeType = "tif";
}
}
if(mimeType.contains("png")){
mimeType ="png";
}
else if( mimeType.contains("jpg")||mimeType.contains("jpeg")){
mimeType = "jpg";
}
else if (mimeType.contains("pdf")){
mimeType ="pdf";
}
else if(mimeType.contains("tif")||mimeType.contains("tiff")){
mimeType = "tif";
}
currentImageType = ImageType.fromValue(mimeType);
}
Tried below code and it worked.
public static boolean isSelectedFilePdf(Uri uri, ContentResolver contentResolver) {
if (uri != null) {
if (uri.getScheme().equals("content")) {
String type = contentResolver.getType(uri);
return type != null && type.startsWith("application/pdf");
} else {
String fileName = uri.getLastPathSegment();
String extension = fileName.substring(fileName.lastIndexOf("."));
return extension != null && extension.equalsIgnoreCase(".pdf");
}
}
}
The following solution is mentioned at Check whether a PDF-File is valid (Python)
In a project if mine I need to check for the mime type of some uploaded file. I simply use the file command like this:
from subprocess import Popen, PIPE
filetype = Popen("/usr/bin/file -b --mime -", shell=True, stdout=PIPE, stdin=PIPE).communicate(file.read(1024))[0].strip()
You of course might want to move the actual command into some configuration file as also command line options vary among operating systems (e.g. mac).
If you just need to know whether it's a PDF or not and do not need to process it anyway I think the file command is a faster solution than a lib. Doing it by hand is of course also possible but the file command gives you maybe more flexibility if you want to check for different types.
This might sound a little bit too obvious, but check the extension on the filename.
If it's good enough for explorer, it should be good enough for you

Load and display all the images from a folder

I want to read all the images in a folder using Java.
When: I press a button in the Java application,
It should:
ask for the directory's path in a popup,
then load all the images from this directory,
then display their names, dimension types and size.
How to proceed?
I have the code for read the image and also for all image in the folder but how the things i told above can be done?
Any suggestion or help is welcome! Please provide reference links!
Untested because not on a machine with a JDK installed, so bear with me, that's all typed-in "as-is", but should get you started (expect a rush of downvotes...)
Loading all the Images from a Folder
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Test {
// File representing the folder that you select using a FileChooser
static final File dir = new File("PATH_TO_YOUR_DIRECTORY");
// array of supported extensions (use a List if you prefer)
static final String[] EXTENSIONS = new String[]{
"gif", "png", "bmp" // and other formats you need
};
// filter to identify images based on their extensions
static final FilenameFilter IMAGE_FILTER = new FilenameFilter() {
#Override
public boolean accept(final File dir, final String name) {
for (final String ext : EXTENSIONS) {
if (name.endsWith("." + ext)) {
return (true);
}
}
return (false);
}
};
public static void main(String[] args) {
if (dir.isDirectory()) { // make sure it's a directory
for (final File f : dir.listFiles(IMAGE_FILTER)) {
BufferedImage img = null;
try {
img = ImageIO.read(f);
// you probably want something more involved here
// to display in your UI
System.out.println("image: " + f.getName());
System.out.println(" width : " + img.getWidth());
System.out.println(" height: " + img.getHeight());
System.out.println(" size : " + f.length());
} catch (final IOException e) {
// handle errors here
}
}
}
}
}
APIs Used
This is relatively simple to do and uses only standard JDK-packaged classes:
File
FilenameFilter
BufferedImage
ImageIO
These sessions of the Java Tutorial might help you as well:
Reading/Loading an Image
How to Use Icons
How to Use File Choosers
Possible Enhancements
Use Apache Commons FilenameUtils to extract files' extensions
Detect files based on actual mime-types or content, not based on extensions
I leave UI code up to you. As I'm unaware if this is homework or not, I don't want to provide a full solution. But to continue:
Look at a FileChooser to select the folder.
I assume you already know how to make frames/windows/dialogs.
Read the Java Tutorial How to Use Icons sections, which teaches you how to display and label them.
I left out some issues to be dealt with:
Exception handling
Folders with evil endigs (say you have a folder "TryMeIAmEvil.png")
By combining all of the above, it's pretty easy to do.
javaxt.io.Directory directory = new javaxt.io.Directory("C:\Users\Public\Pictures\Sample Pictures");
directory.getFiles();
javaxt.io.File[] files;
java.io.FileFilter filter = file -> !file.isHidden() && (file.isDirectory() || (file.getName().endsWith(".jpg")));
files = directory.getFiles(filter, true);
System.out.println(Arrays.toString(files));
step 1=first of all make a folder out of webapps
step2= write code to uploading a image in ur folder
step3=write a code to display a image in ur respective jsp,html,jframe what u want
this is folder=(images)
reading image for folder'
Image image = null;
try {
File sourceimage = new File("D:\\images\\slide4.jpg");
image = ImageIO.read(sourceimage);
} catch (IOException e) {
e.printStackTrace();
}
}

Categories

Resources