📄 lljtrantutorial.java
字号:
* this java reads the cropped image directly from LLJTran using directio. * Then the image is scaled to thumbnail size and this image is set as the * new Thumbnail<br> * 4. Saves the transormed Image to outputFile with the new Thumbnail */ public static void main2(String[] args) throws Exception { if(args.length != 6) { System.out.println("Usage: java LLJTranTutorial <inputFile> <outputFile> <cropX> <cropY> <cropWidth> <cropHeight>"); System.exit(1); } // 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. Crop it to the specified Bounds Rectangle cropArea = new Rectangle(); cropArea.x = Integer.parseInt(args[2]); cropArea.y = Integer.parseInt(args[3]); cropArea.width = Integer.parseInt(args[4]); cropArea.height = Integer.parseInt(args[5]); llj.transform(LLJTran.CROP, LLJTran.OPT_DEFAULTS, cropArea); // 3. If Image has an Exif header set/change the Thumbnail to the // downscale of new Image if(llj.getImageInfo() instanceof Exif) { // Read the image in llj and get a Thumbnail Image from it. // // In the regular usage you can save the image in llj to an // OutputStream. // // However since llj implements an IterativeWriter the image can be // directly read InStreamFromIterativeWriter iwip = new InStreamFromIterativeWriter(); IterativeWriter iWriter = llj.initWrite(iwip.getWriterOutputStream(), LLJTran.NONE, LLJTran.OPT_WRITE_ALL, null, 0, false); iwip.setIterativeWriter(iWriter); byte newThumbnail[] = getThumbnailImage(iwip); llj.wrapupIterativeWrite(iWriter); // Set the new Thumbnail if(llj.setThumbnail(newThumbnail, 0, newThumbnail.length, ImageResources.EXT_JPG)) System.out.println("Successfully Set New Thumbnail"); else System.out.println("Error Setting New Thumbnail"); } else System.out.println("Cannot Set Thumbnail Since There is no EXIF Header"); // 4. Save the Image with the new Thumbnail OutputStream out = new BufferedOutputStream( new FileOutputStream(args[1])); llj.save(out, LLJTran.OPT_WRITE_ALL); out.close(); // Cleanup llj.freeMemory(); } /** * This example demonstrates modifyig just the Exif Header using xferInfo. * It also shows the use of SplitInputStream for LLJTran to Share the Image * Input with jdk's ImageReader.<p> * * Usage: java LLJTranTutorial <inputFile> <outputFile><p> * * inputFile must be a jpeg file without Exif or with Exif but without a * Thumbnail image. * * The program does the following:<p> * * 1. Reads the Image from inputFile upto READ_HEADER along with the * ImageReader using SplitInputStream<br> * 2. If the image has a Thumbnail (Which means it has a Exif Header) it * prints a message and exits.<br> * 3. If the Image does not have an Exif Header it creates a dummy Exif * Header<br> * 4. The image is scaled to thumbnail size and this image is set as the * new Thumbnail * 5. Transfers the image from inputFile to outputFile replacing the new * Exif with the Thumbnail so that outputFile has a Thumbnail. */ public static void main3(String[] args) throws Exception { if(args.length != 2) { System.out.println("Usage: java LLJTranTutorial <inputFile> <outputFile>"); System.exit(1); } // 1. Read the Image from inputFile upto READ_HEADER along with the // ImageReader using SplitInputStream and Generate a Thumbnail from // the Image. InputStream fip = new FileInputStream(args[0]); // No need to buffer SplitInputStream sip = new SplitInputStream(fip); // Create a substream for LLJTran to use InputStream subIp = sip.createSubStream(); LLJTran llj = new LLJTran(subIp); // Normally it would be better to read the entire image when reading // shared. But LLJTran only needs to read upto header for loading // imageInfo and using xferInfo. llj.initRead(LLJTran.READ_HEADER, true, true); sip.attachSubReader(llj, subIp); // LLJTran reads the image when the below API reads from sip via // nextRead() calls made by sip. byte newThumbnail[] = getThumbnailImage(sip); sip.wrapup(); fip.close(); // Check llj for errors String msg = llj.getErrorMsg(); if(msg != null) { System.out.println("Error in LLJTran While Loading Image: " + msg); Exception e = llj.getException(); if(e != null) { System.out.println("Got an Exception, throwing it.."); throw e; } System.exit(1); } // 2. If the image has a Thumbnail (Which means it has a Exif Header) // print a message and exit. AbstractImageInfo imageInfo = llj.getImageInfo(); if(imageInfo.getThumbnailLength() > 0) { System.out.println("Image already has a Thumbnail. Exitting.."); System.exit(1); } // 3. If the Image does not have an Exif Header create a dummy Exif // Header if(!(imageInfo instanceof Exif)) { System.out.println("Adding a Dummy Exif Header"); llj.addAppx(LLJTran.dummyExifHeader, 0, LLJTran.dummyExifHeader.length, true); imageInfo = llj.getImageInfo(); // This would have changed Exif exif = (Exif) imageInfo; // Changed Date/Time and dimensions in Dummy Exif Entry entry = exif.getTagValue(Exif.DATETIME, true); if(entry != null) entry.setValue(0, "1998:08:18 11:15:00"); entry = exif.getTagValue(Exif.DATETIMEORIGINAL, true); if(entry != null) entry.setValue(0, "1998:08:18 11:15:00"); entry = exif.getTagValue(Exif.DATETIMEDIGITIZED, true); if(entry != null) entry.setValue(0, "1998:08:18 11:15:00"); int imageWidth = llj.getWidth(); int imageHeight = llj.getHeight(); if(imageWidth > 0 && imageHeight > 0) { entry = exif.getTagValue(Exif.EXIFIMAGEWIDTH, true); if(entry != null) entry.setValue(0, new Integer(imageWidth)); entry = exif.getTagValue(Exif.EXIFIMAGELENGTH, true); if(entry != null) entry.setValue(0, new Integer(imageHeight)); } } // 4. Set the new Thumbnail if(llj.setThumbnail(newThumbnail, 0, newThumbnail.length, ImageResources.EXT_JPG)) System.out.println("Successfully Set New Thumbnail"); else System.out.println("Error Setting New Thumbnail"); // 5. Transfer the image from inputFile to outputFile replacing the new // Exif with the Thumbnail so that outputFile has a Thumbnail. fip = new BufferedInputStream(new FileInputStream(args[0])); OutputStream out = new BufferedOutputStream( new FileOutputStream(args[1])); // Replace the new Exif Header in llj while copying the image from fip // to out llj.xferInfo(fip, out, LLJTran.REPLACE, LLJTran.RETAIN); fip.close(); out.close(); // Cleanup llj.freeMemory(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -