Java Tiff Image Jpeg Image Png Image Convert
package com.store.file;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Random;
import javax.imageio.ImageIO;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.protocol.HTTP;
import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.SeekableStream;
import com.sun.media.jai.codec.TIFFDecodeParam;
import javax.media.jai.JAI;
import javax.media.jai.NullOpImage;
import javax.media.jai.OpImage;
import javax.media.jai.PlanarImage;
public class PutImageIntoXml {
public static int DISPLAY_WIDTH = 100;
public static String transferImage(String storeLocation, String imageName,
String claimNumber) throws IOException {
ByteArrayOutputStream stream = null;
String imageXmlFile = null;
System.out.println("\nStoreLocation:" + storeLocation);
System.out.println("\nImage Name:" + imageName);
String[] contents = imageName.split("\\.");
if (contents.length == 2) {
if (contents[1].equals("tiff") || contents[1].equals("tif")) {
try {
SeekableStream s = new FileSeekableStream(storeLocation
+ imageName);
TIFFDecodeParam param = null;
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s,
param);
ArrayList<ByteArrayOutputStream> bytecontents= new ArrayList<ByteArrayOutputStream>();
int numofpages = dec.getNumPages();
System.out.println(numofpages);
for (int i=0; i< numofpages; i++) {
RenderedImage op = new NullOpImage(dec.decodeAsRenderedImage(i),null,null,OpImage.OP_COMPUTE_BOUND);
stream = new ByteArrayOutputStream();
int width = op.getWidth();
int height = op.getHeight();
double conversionFactor = (double)DISPLAY_WIDTH / (double)width;
float conversionFactor_ = DISPLAY_WIDTH / width;
int thumbHeight = (int)((double)height * conversionFactor);
int thumbWidth = (int)((double)width * conversionFactor);
Dimension dim = new Dimension(thumbHeight, thumbWidth);
JAI.setDefaultRenderingSize(dim);
JAI.setDefaultTileSize(dim);
JAI.create("encode", op, stream, "tiff");
bytecontents.add(stream);
}
StringBuffer encodedImage = new StringBuffer();
int numberofpages = bytecontents.size();
for(int i=0;i<numberofpages;i++){
stream = bytecontents.get(i);
byte[] b = stream.toByteArray();
encodedImage =encodedImage.append(Base64.encodeBase64String(b));
}
imageXmlFile =createImageXML(storeLocation, encodedImage.toString(),
claimNumber);
System.out.println(imageXmlFile);
imageXmlFile=URLEncoder.encode(imageXmlFile,HTTP.UTF_8);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Some problem with image");
} finally {
stream.close();
}
} else {
try {
BufferedImage img = ImageIO.read(new File(storeLocation
+ imageName));
stream = new ByteArrayOutputStream();
ImageIO.write(img, "png", stream);
stream.flush();
String encodedImage = Base64.encodeBase64String(stream
.toByteArray());
imageXmlFile = createImageXML(storeLocation, encodedImage,
claimNumber);
} catch (Exception e) {
} finally {
stream.close();
}
return imageXmlFile;
}
}
return imageXmlFile;
}
public static String createImageXML(String storeLocation,
String encodedImage, String claimNumber) {
StringBuffer out = null;
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(100);
try {
String timeFileSent = sdf.format(cal.getTime());
out = new StringBuffer();
out.append("<ABC>");
out.append("<Document>");
out.append(encodedImage);
out.append("</Document>");
out.append("<WorkType>Documentation</WorkType>");
out.append(" </ABC>");
System.out.println("\nWrote the file");
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
} finally {
}
return out.toString();
}
public static String[] splitStoreLocationAndImageName(String imageName) {
String[] storeAndImage = new String[2];
System.out.println("\nImageName:" + imageName);
int lastIndex = imageName.lastIndexOf("/");
System.out.println(lastIndex);
storeAndImage[0] = imageName.substring(0, lastIndex + 1);
System.out.println();
storeAndImage[1] = (imageName.substring(lastIndex + 1));
return storeAndImage;
}
public static final String DATE_FORMAT_NOW = "yyyy-MM-ddHH:mm:ss:";
}
No comments:
Post a Comment