public void SetReader(BinaryReader jpegFileReader)
 {
     TiffHeaderOffset = 12;
     _jpegFileReader = jpegFileReader;
     _binaryReader = new BigEndianBinaryReader(_jpegFileReader);
     
 }
Beispiel #2
0
        public ExifPhoto GetPhotoInfo()
        {
            var fileHeader = _jpegFileReader.ReadBytes(2);

            if (!IsJpeg(fileHeader))
            {
                throw new InvalidDataException("File is not a recognisable jpeg file");
            }
            var nextHeader = _jpegFileReader.ReadBytes(2);

            if (!IsExif(nextHeader))
            {
                if (!IsJfif(nextHeader))
                {
                    throw new InvalidDataException("File does not start with JPEG Exif header");
                }
                var jfifLength = _binaryReader.ReadUInt16();
                _jpegFileReader.BaseStream.Seek(jfifLength - 2, SeekOrigin.Current);
                nextHeader = _jpegFileReader.ReadBytes(2);
                if (!IsExif(nextHeader))
                {
                    throw new InvalidDataException("Could not find Exif header");
                }
                TiffHeaderOffset = _jpegFileReader.BaseStream.Position + 8;
            }

            if (!IsBigEndian())
            {
                _binaryReader = new LittleEndianBinaryReader(_jpegFileReader);
            }

            var photo = new ExifPhoto();

            ReadIFDHeader(ExifHeader.IFD0, 8, photo);

            if (photo.SubIFDPosition == 0)
            {
                throw new InvalidDataException("Have not found ExifSubIdTag inside the file");
            }

            ReadIFDHeader(ExifHeader.SubIFD, photo.SubIFDPosition, photo);

            if (photo.Manufacturer == null || photo.Model == null)
            {
                throw new InvalidDataException("Could not read photo manufacturer or model from the Exif header");
            }

            return(photo);
        }
Beispiel #3
0
 public void SetReader(BinaryReader jpegFileReader)
 {
     TiffHeaderOffset = 12;
     _jpegFileReader  = jpegFileReader;
     _binaryReader    = new BigEndianBinaryReader(_jpegFileReader);
 }
        public ExifPhoto GetPhotoInfo()
        {
            var fileHeader = _jpegFileReader.ReadBytes(2);
            if (!IsJpeg(fileHeader)) throw new InvalidDataException("File is not a recognisable jpeg file");
            var nextHeader = _jpegFileReader.ReadBytes(2);
            if (!IsExif(nextHeader)) {
                if (!IsJfif(nextHeader))
                    throw new InvalidDataException("File does not start with JPEG Exif header");
                var jfifLength = _binaryReader.ReadUInt16();
                _jpegFileReader.BaseStream.Seek(jfifLength - 2, SeekOrigin.Current);
                nextHeader = _jpegFileReader.ReadBytes(2);
                if (!IsExif(nextHeader)) throw new InvalidDataException("Could not find Exif header");
                TiffHeaderOffset = _jpegFileReader.BaseStream.Position + 8;
            }

            if (!IsBigEndian()) _binaryReader = new LittleEndianBinaryReader(_jpegFileReader);

            var photo = new ExifPhoto();
            ReadIFDHeader(ExifHeader.IFD0, 8, photo);

            if (photo.SubIFDPosition == 0)
                throw new InvalidDataException("Have not found ExifSubIdTag inside the file");

            ReadIFDHeader(ExifHeader.SubIFD, photo.SubIFDPosition, photo);

            if (photo.Manufacturer == null || photo.Model == null)
                throw new InvalidDataException("Could not read photo manufacturer or model from the Exif header");

            return photo;
        }