jarturon55

convert to tiff

Jan 24th, 2021
738
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static byte[] imageIOConvertTIFFG4WithDpi(byte[] imageByte, boolean isOneBitColor, Integer dpiX, Integer dpiY) throws IOException, ImageReadException {
  2.        
  3.         BufferedImage input = ImageConvertUtil.imageIOConvertBufferdImage(imageByte);
  4.        
  5.         // 1-bit color convert
  6.         if (isOneBitColor) {
  7.             input = ConvertUtil.convert1(input);
  8.         }
  9.        
  10.         Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("TIFF");
  11.         while(writers.hasNext()) {
  12.             ImageWriter writer = writers.next();
  13.             ImageWriteParam iwp = writer.getDefaultWriteParam();
  14.             iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
  15.             iwp.setCompressionType("CCITT T.6");
  16.            
  17.             IIOMetadata streamMetadata = createMetadata(writer, iwp, dpiX, dpiY);
  18.  
  19.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  20.             javax.imageio.stream.MemoryCacheImageOutputStream output = new MemoryCacheImageOutputStream(bout);
  21.             writer.setOutput(output);
  22.             IIOImage image = new IIOImage(input, null, streamMetadata);
  23.             writer.write(null, image, iwp);
  24.             output.close();
  25.  
  26.             return bout.toByteArray();
  27.         }
  28.  
  29.         return null;
  30.     }
  31.  
  32.  
  33.  
  34.  
  35.     private static IIOMetadata createMetadata(ImageWriter writer, ImageWriteParam writerParams, Integer widthDpi, Integer heightDpi) throws
  36.     IIOInvalidTreeException
  37.     {
  38.     // Get default metadata from writer
  39.     ImageTypeSpecifier type = writerParams.getDestinationType();
  40.     IIOMetadata meta = writer.getDefaultImageMetadata(type, writerParams);
  41.    
  42.     // Convert default metadata to TIFF metadata
  43.     TIFFDirectory dir = TIFFDirectory.createFromMetadata(meta);
  44.    
  45.     // Get {X,Y} resolution tags
  46.     BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();
  47.     TIFFTag tagXRes = base.getTag(BaselineTIFFTagSet.TAG_X_RESOLUTION);
  48.     TIFFTag tagYRes = base.getTag(BaselineTIFFTagSet.TAG_Y_RESOLUTION);
  49.     long dpiX = widthDpi.longValue();
  50.     long dpiY = heightDpi.longValue();
  51.     // Create {X,Y} resolution fields
  52.     TIFFField fieldXRes = new TIFFField(tagXRes, TIFFTag.TIFF_RATIONAL, 1, new long[][] { { dpiX, 1l } });
  53.     TIFFField fieldYRes = new TIFFField(tagYRes, TIFFTag.TIFF_RATIONAL, 1, new long[][] { { dpiY, 1l } });
  54.    
  55.     // Add {X,Y} resolution fields to TIFFDirectory
  56.     dir.addTIFFField(fieldXRes);
  57.     dir.addTIFFField(fieldYRes);
  58.    
  59.     // Add unit field to TIFFDirectory (change to RESOLUTION_UNIT_CENTIMETER if necessary)
  60.     dir.addTIFFField(new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_RESOLUTION_UNIT), BaselineTIFFTagSet.RESOLUTION_UNIT_INCH));
  61.    
  62.     // Return TIFF metadata so it can be picked up by the IIOImage
  63.     return dir.getAsMetadata();
  64.     }
  65.  
  66.  
RAW Paste Data