javax.imageio.plugins.tiff.TIFFTag#TIFF_RATIONAL源码实例Demo

下面列出了javax.imageio.plugins.tiff.TIFFTag#TIFF_RATIONAL 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: MyBox   文件: ImageTiffFile.java
public static IIOMetadata getWriterMeta(ImageAttributes attributes, BufferedImage image,
        ImageWriter writer, ImageWriteParam param) {
    try {
        IIOMetadata metaData;
        if (attributes != null && attributes.getColorType() != null) {
            ImageTypeSpecifier imageTypeSpecifier;
            switch (attributes.getColorType()) {
                case ARGB:
                    imageTypeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB);
                    break;
                case RGB:
                    imageTypeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
                    break;
                case BINARY:
                    imageTypeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_BYTE_BINARY);
                    break;
                case GRAY:
                    imageTypeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_BYTE_GRAY);
                    break;
                default:
                    imageTypeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
                    break;
            }
            metaData = writer.getDefaultImageMetadata(imageTypeSpecifier, param);
        } else if (image != null) {
            metaData = writer.getDefaultImageMetadata(new ImageTypeSpecifier(image), param);
        } else {
            metaData = writer.getDefaultImageMetadata(
                    ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB), param);
        }

        if (metaData == null || metaData.isReadOnly() || attributes == null) {
            return metaData;
        }
        if (attributes.getDensity() > 0) {
            String nativeName = metaData.getNativeMetadataFormatName();  // javax_imageio_tiff_image_1.0
            Node nativeTree = metaData.getAsTree(nativeName);
            long[] xRes = new long[]{attributes.getDensity(), 1};
            long[] yRes = new long[]{attributes.getDensity(), 1};
            TIFFField fieldXRes = new TIFFField(
                    BaselineTIFFTagSet.getInstance().getTag(BaselineTIFFTagSet.TAG_X_RESOLUTION),
                    TIFFTag.TIFF_RATIONAL, 1, new long[][]{xRes});
            TIFFField fieldYRes = new TIFFField(
                    BaselineTIFFTagSet.getInstance().getTag(BaselineTIFFTagSet.TAG_Y_RESOLUTION),
                    TIFFTag.TIFF_RATIONAL, 1, new long[][]{yRes});
            nativeTree.getFirstChild().appendChild(fieldXRes.getAsNativeNode());
            nativeTree.getFirstChild().appendChild(fieldYRes.getAsNativeNode());
            char[] fieldUnit = new char[]{BaselineTIFFTagSet.RESOLUTION_UNIT_INCH};
            TIFFField fieldResUnit = new TIFFField(
                    BaselineTIFFTagSet.getInstance().getTag(BaselineTIFFTagSet.TAG_RESOLUTION_UNIT),
                    TIFFTag.TIFF_SHORT, 1, fieldUnit);
            nativeTree.getFirstChild().appendChild(fieldResUnit.getAsNativeNode());
            metaData.mergeTree(nativeName, nativeTree);
        }

        return metaData;

    } catch (Exception e) {
        logger.error(e.toString());
        return null;
    }
}
 
源代码2 项目: Bytecoder   文件: TIFFIFD.java
private static void writeTIFFFieldToStream(TIFFField field,
                                           ImageOutputStream stream)
    throws IOException {
    int count = field.getCount();
    Object data = field.getData();

    switch (field.getType()) {
    case TIFFTag.TIFF_ASCII:
        for (int i = 0; i < count; i++) {
            String s = ((String[])data)[i];
            int length = s.length();
            for (int j = 0; j < length; j++) {
                stream.writeByte(s.charAt(j) & 0xff);
            }
            stream.writeByte(0);
        }
        break;
    case TIFFTag.TIFF_UNDEFINED:
    case TIFFTag.TIFF_BYTE:
    case TIFFTag.TIFF_SBYTE:
        stream.write((byte[])data);
        break;
    case TIFFTag.TIFF_SHORT:
        stream.writeChars((char[])data, 0, ((char[])data).length);
        break;
    case TIFFTag.TIFF_SSHORT:
        stream.writeShorts((short[])data, 0, ((short[])data).length);
        break;
    case TIFFTag.TIFF_SLONG:
        stream.writeInts((int[])data, 0, ((int[])data).length);
        break;
    case TIFFTag.TIFF_LONG:
        for (int i = 0; i < count; i++) {
            stream.writeInt((int)(((long[])data)[i]));
        }
        break;
    case TIFFTag.TIFF_IFD_POINTER:
        stream.writeInt(0); // will need to be backpatched
        break;
    case TIFFTag.TIFF_FLOAT:
        stream.writeFloats((float[])data, 0, ((float[])data).length);
        break;
    case TIFFTag.TIFF_DOUBLE:
        stream.writeDoubles((double[])data, 0, ((double[])data).length);
        break;
    case TIFFTag.TIFF_SRATIONAL:
        for (int i = 0; i < count; i++) {
            stream.writeInt(((int[][])data)[i][0]);
            stream.writeInt(((int[][])data)[i][1]);
        }
        break;
    case TIFFTag.TIFF_RATIONAL:
        for (int i = 0; i < count; i++) {
            long num = ((long[][])data)[i][0];
            long den = ((long[][])data)[i][1];
            stream.writeInt((int)num);
            stream.writeInt((int)den);
        }
        break;
    default:
        // error
    }
}
 
源代码3 项目: openjdk-jdk9   文件: TIFFIFD.java
private static void writeTIFFFieldToStream(TIFFField field,
                                           ImageOutputStream stream)
    throws IOException {
    int count = field.getCount();
    Object data = field.getData();

    switch (field.getType()) {
    case TIFFTag.TIFF_ASCII:
        for (int i = 0; i < count; i++) {
            String s = ((String[])data)[i];
            int length = s.length();
            for (int j = 0; j < length; j++) {
                stream.writeByte(s.charAt(j) & 0xff);
            }
            stream.writeByte(0);
        }
        break;
    case TIFFTag.TIFF_UNDEFINED:
    case TIFFTag.TIFF_BYTE:
    case TIFFTag.TIFF_SBYTE:
        stream.write((byte[])data);
        break;
    case TIFFTag.TIFF_SHORT:
        stream.writeChars((char[])data, 0, ((char[])data).length);
        break;
    case TIFFTag.TIFF_SSHORT:
        stream.writeShorts((short[])data, 0, ((short[])data).length);
        break;
    case TIFFTag.TIFF_SLONG:
        stream.writeInts((int[])data, 0, ((int[])data).length);
        break;
    case TIFFTag.TIFF_LONG:
        for (int i = 0; i < count; i++) {
            stream.writeInt((int)(((long[])data)[i]));
        }
        break;
    case TIFFTag.TIFF_IFD_POINTER:
        stream.writeInt(0); // will need to be backpatched
        break;
    case TIFFTag.TIFF_FLOAT:
        stream.writeFloats((float[])data, 0, ((float[])data).length);
        break;
    case TIFFTag.TIFF_DOUBLE:
        stream.writeDoubles((double[])data, 0, ((double[])data).length);
        break;
    case TIFFTag.TIFF_SRATIONAL:
        for (int i = 0; i < count; i++) {
            stream.writeInt(((int[][])data)[i][0]);
            stream.writeInt(((int[][])data)[i][1]);
        }
        break;
    case TIFFTag.TIFF_RATIONAL:
        for (int i = 0; i < count; i++) {
            long num = ((long[][])data)[i][0];
            long den = ((long[][])data)[i][1];
            stream.writeInt((int)num);
            stream.writeInt((int)den);
        }
        break;
    default:
        // error
    }
}