// Scale the image to around 160x120/120x160 pixels, may not conform // exactly to Thumbnail requirements of 160x120. int t, longer, shorter; longer = image.getWidth(); shorter = image.getHeight(); if(shorter > longer) { t = longer; longer = shorter; shorter = t; } double factor = 160/(double)longer; double factor1 = 120/(double)shorter; if(factor1 > factor) factor = factor1; AffineTransform tx = new AffineTransform(); tx.scale(factor, factor); AffineTransformOp affineOp = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); image = affineOp.filter(image, null);
// Write Out the Scaled Image to a ByteArrayOutputStream and return the // bytes ByteArrayOutputStream byteStream = new ByteArrayOutputStream(2048); String format = "JPG"; ImageIO.write(image, format, byteStream);
return byteStream.toByteArray(); }
/** * This example demonstrates basic Lossless Transformation. <p> * * Usage: java LLJTranTutorial <inputFile> <outputFile> <transformOperation> <p> * * The program does the following: <p> * * 1. Reads the Image from inputFile <br> * 2. Transforms it with the specified transformOperation except CROP <br> * 3. Saves a vertical mirror of this transformed Image into vmirror.jpg * removing any Exif header information <br> * 4. Saves the transormed Image (The Vertical Mirroring is not included) to * outputFile */ public static void main1(String[] args) throws Exception { int op = 0;
System.out.println("Usage: java LLJTranTutorial <inputFile> <outputFile> <transformOperation>\n"); System.out.println(" Use the following codes for <transformOperation>:"); System.out.println(" 0: NONE"); System.out.println(" 1: FLIP_H"); System.out.println(" 2: FLIP_V"); System.out.println(" 3: TRANSPOSE"); System.out.println(" 4: TRANSVERSE"); System.out.println(" 5: ROTATE 90 degrees clockwise"); System.out.println(" 6: ROTATE 180 degrees clockwise"); System.out.println(" 7: ROTATE 270 degrees clockwise"); System.out.println("Also creates vmirror.jpg, Vertical mirror of outputFile without Exif"); System.exit(1); }
// Raise the Debug Level which is normally LEVEL_INFO. Only Warning // messages will be printed by MediaUtil. Log.debugLevel = Log.LEVEL_WARNING;
// 1. Initialize LLJTran and Read the entire Image including Appx markers LLJTran llj = new LLJTran(new File(args[0])); // If you pass the 2nd parameter as false, Exif information is not // loaded and hence will not be written. llj.read(LLJTran.READ_ALL, true);
// 2. Transform the image using default options along with // transformation of the Orientation tags. Try other combinations of // LLJTran_XFORM.. flags. Use a jpeg with partial MCU (partialMCU.jpg) // for testing LLJTran.XFORM_TRIM and LLJTran.XFORM_ADJUST_EDGES int options = LLJTran.OPT_DEFAULTS ¦ LLJTran.OPT_XFORM_ORIENTATION; llj.transform(op, options);
// 3. Save the Vertical mirror of the Transformed image without Exif // header. OutputStream out = new BufferedOutputStream( new FileOutputStream("vmirror.jpg")); // Turn off OPT_WRITE_APPXS flag to Skip writing Exif. // Also try writing with Appxs options = LLJTran.OPT_DEFAULTS & ~LLJTran.OPT_WRITE_APPXS; // Save with vertical transformation without changing the llj image. llj.transform(out, LLJTran.FLIP_V, options); out.close();
// 4. Save the Image which is already transformed as specified by the // input transformation in Step 2, along with the Exif header. out = new BufferedOutputStream( new FileOutputStream(args[1])); llj.save(out, LLJTran.OPT_WRITE_ALL); out.close();