Wednesday, March 25, 2009

Image compressor

Found this piece of code on www.coderanch.com

package com;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

public class ImageCompression
{
public static void main(String[] args) throws IOException
{
Iterator writers;
BufferedImage bufferedImage;
ImageOutputStream imageOutputStream;
ImageWriter imageWriter;
ImageWriteParam pngparams;
// Read an image from the disk (First Argument)
// bufferedImage = ImageIO.read(new File(args[0]));
bufferedImage = ImageIO.read(new File("C:\\DSC0009111370.JPG"));
// Get all the PNG writers
writers = ImageIO.getImageWritersByFormatName("jpg");
// Fetch the first writer in the list
imageWriter = (ImageWriter) writers.next();
// Just to confirm that the writer in use is CLibPNGImageWriter
System.out.println("\n Writer used : " + imageWriter.getClass().getName() + "\n");
// Specify the parameters according to those the output file will be
// written
// Get Default parameters
pngparams = imageWriter.getDefaultWriteParam();
// Define compression mode
pngparams.setCompressionMode(javax.imageio.ImageWriteParam.MODE_EXPLICIT);
// Define compression quality
pngparams.setCompressionQuality(0.5F);
// Define progressive mode
pngparams.setProgressiveMode(javax.imageio.ImageWriteParam.MODE_COPY_FROM_METADATA);
// Deine destination type - used the ColorModel and SampleModel of the
// Input Image
pngparams.setDestinationType(new ImageTypeSpecifier(bufferedImage.getColorModel(), bufferedImage
.getSampleModel()));
// Set the output stream to Second Argument
// imageOutputStream = ImageIO.createImageOutputStream( new
// FileOutputStream(args[1]) );
imageOutputStream = ImageIO.createImageOutputStream(new FileOutputStream("C:\\new_test.jpg"));
imageWriter.setOutput(imageOutputStream);
// Write the changed Image
imageWriter.write(null, new IIOImage(bufferedImage, null, null), pngparams);
// Close the streams
imageOutputStream.close();
imageWriter.dispose();
}
}

Tuesday, March 17, 2009

SPLIT A TEXT FILE TO A SPECIFIED NO. OF SUBFILES

package com;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class SplitFile
{
/*The method splitFile(File source,int noFile,int lsize) splits the file 'source' into 'noFile' no. of individual files, each file having 'lsize' no. of characters*/

public static File[] splitFile(File source,int noFile,int lsize)
{
File[] fileFragments = new File[noFile];
String[] frgfName = new String[noFile];

try{
String sourceFName = source.getName();
long sourceFSize = source.length();
FileInputStream fis = new FileInputStream(source);

System.out.println(noFile);
if (lsize != 0) {
noFile--;
}
System.out.println(noFile);
sourceFName = sourceFName.substring(0, sourceFName.lastIndexOf("."));
int j=0;
for (int i = 1; i <= noFile; i++) {
frgfName[i-1] = "D:\\"+sourceFName + String.valueOf(i)+".txt";
fileFragments[i-1] = new File(frgfName[i-1]);

FileOutputStream fos = new FileOutputStream(fileFragments[i - 1]);
byte[] data = new byte[lsize];
int count = fis.read(data);
fos.write(data);
fos.close();
String frgFileInfo = new String(frgfName[i-1] + "," + String.valueOf(lsize));
}
if (lsize != 0) {
System.out.println(noFile);
frgfName[noFile] = "D:\\"+sourceFName + String.valueOf(noFile+1)+".txt";
fileFragments[noFile] = new File(frgfName[noFile]);
FileOutputStream fos = new FileOutputStream(fileFragments[noFile]);
byte[] data = new byte[lsize];
int count = fis.read(data);
fos.write(data);
fos.close();
String frgFileInfo = new String(frgfName[noFile] + "," + String.valueOf(lsize));
}

} catch (Exception e) {
System.out.println("Error in Splitting:"+e);
return null;
}
return fileFragments;

}

public static void main(String[] args) {

File keyfile = new File("D:/test.txt"); // the path to the source file
File[] resultFiles=null;
resultFiles=splitFile(keyfile,5,30);
for(File fileObj:resultFiles){
System.out.println(fileObj.getAbsolutePath());
}

}
}