001/* 002 * (C) Copyright 2007 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.platform.picture; 023 024import java.util.Arrays; 025 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028 029/** 030 * Helper to handle the UNDEFINED Exif data type. 031 * <p> 032 * See {@link http ://www.leadtools.com/help/leadtools/v15/Main/API/Dllaux/ExifComments.htm}. 033 * 034 * @author btatar 035 */ 036public class ExifHelper { 037 038 public static final Log log = LogFactory.getLog(ExifHelper.class); 039 040 // the ASCII data format 041 public static final byte[] ASCII = { 65, 83, 67, 73, 73, 0, 0, 0 }; 042 043 // the JIS data format 044 public static final byte[] JIS = { 74, 73, 83, 0, 0, 0, 0, 0 }; 045 046 // the UNDEFINED data format 047 public static final byte[] UNDEFINED = { 0, 0, 0, 0, 0, 0, 0, 0 }; 048 049 private ExifHelper() { 050 } 051 052 /** 053 * Method used to perform the decode of the <b>Exif User comment</b> data type. The first eight bytes specify the 054 * data format, and the remainder of the comment is in the specified format.The first eight bytes can be any of the 055 * following cases: 65, 83, 67, 73, 73, 0, 0, 0 = ASCII 74, 73, 83, 0, 0, 0, 0, 0 = JIS 0, 0, 0, 0, 0, 0, 0, 0 = 056 * UNDEFINED 057 * 058 * @param rawBytes the user comment represented as a byte array 059 * @return the user comment as a String on the format retrieved from the data type. 060 */ 061 public static String decodeUndefined(byte[] rawBytes) { 062 063 byte[] dataType = extractBytes(rawBytes, 0, 8); 064 if (Arrays.equals(ASCII, dataType)) { 065 if (rawBytes.length <= 8) { 066 return ""; 067 } 068 return new String(extractBytes(rawBytes, 8, rawBytes.length - 1)); 069 } else if (Arrays.equals(JIS, dataType)) { 070 log.warn("The Japanese data type encoding is not supported yet"); 071 return ""; 072 } else if (Arrays.equals(UNDEFINED, dataType)) { 073 log.debug("Undefined data type encoding"); 074 return ""; 075 } else { 076 log.debug("Unknown data type encoding"); 077 return ""; 078 } 079 } 080 081 /** 082 * Extracts the bytes from the received byte array. The first argument represents the starting location (zero-based) 083 * and the second argument represent the ending location which is not zero based. 084 * 085 * @param bytes the byte array 086 * @param beginIndex the begin index which is zero based 087 * @param endIndex the end index which is not zero based 088 */ 089 public static byte[] extractBytes(byte[] bytes, int beginIndex, int endIndex) { 090 byte[] result = new byte[endIndex - beginIndex]; 091 int count = 0; 092 for (int i = beginIndex; i < endIndex; i++) { 093 result[count++] = bytes[i]; 094 } 095 return result; 096 } 097 098}