private Bitmap StripQuickRouteHeader(Stream stream) { Bitmap targetImage = null; if (stream != null) { // check if the image is an exported QuickRoute image; if yes, remove the border var ed = QuickRouteJpegExtensionData.FromStream(stream); var sourceImage = System.Drawing.Image.FromStream(stream); rawData = new byte[stream.Length]; stream.Position = 0; stream.Read(rawData, 0, (int)stream.Length); if (ed != null) { targetImage = new Bitmap(ed.MapLocationAndSizeInPixels.Width, ed.MapLocationAndSizeInPixels.Height); var g = Graphics.FromImage(targetImage); g.DrawImage(sourceImage, new Rectangle(new Point(0, 0), ed.MapLocationAndSizeInPixels.Size), ed.MapLocationAndSizeInPixels, GraphicsUnit.Pixel); g.Dispose(); } else { targetImage = new Bitmap(sourceImage); } stream.Close(); stream.Dispose(); } return(targetImage); }
public static QuickRouteJpegExtensionData FromQuickRouteDocument(Document document) { var data = new QuickRouteJpegExtensionData { Version = currentVersion, Sessions = document.Sessions, PercentualSize = 1 }; return(data); }
public static QuickRouteJpegExtensionData FromQuickRouteDocument(Document document, SessionCollection sessions, Rectangle imageBounds, Rectangle mapBounds, double percentualSize) { var data = new QuickRouteJpegExtensionData { Version = currentVersion, ImageCornerPositions = document.GetImageCornersLongLat(imageBounds, mapBounds, percentualSize), MapCornerPositions = document.GetMapCornersLongLat(), MapLocationAndSizeInPixels = mapBounds, Sessions = sessions, PercentualSize = percentualSize }; return(data); }
/// <summary> /// Opens a document from a jpeg file with embedded QuickRoute Jpeg Extension Data. /// </summary> /// <param name="fileName">The file name of the jpeg file.</param> /// <param name="settings">The document settings to apply.</param> /// <returns>A QuickRoute document if the jpeg file contains QuickRoute Jpeg Extension Data, otherwise null.</returns> public static Document OpenFromJpeg(string fileName, DocumentSettings settings) { var ed = QuickRouteJpegExtensionData.FromJpegFile(fileName); if (ed == null) { return(null); } var mapAndBorderImage = (Bitmap)Image.FromFile(fileName); var mapImage = new Bitmap(ed.MapLocationAndSizeInPixels.Width, ed.MapLocationAndSizeInPixels.Height); using (var g = Graphics.FromImage(mapImage)) { g.DrawImage(mapAndBorderImage, new Rectangle(0, 0, ed.MapLocationAndSizeInPixels.Width, ed.MapLocationAndSizeInPixels.Height), ed.MapLocationAndSizeInPixels, GraphicsUnit.Pixel); } foreach (var pi in mapAndBorderImage.PropertyItems) { mapImage.SetPropertyItem(pi); } var exif = new ExifWorks.ExifWorks(ref mapAndBorderImage); var qualityByteArray = exif.GetProperty((int)ExifWorks.ExifWorks.TagNames.JPEGQuality, new byte[] { 80 }); var encodingInfo = new JpegEncodingInfo((double)qualityByteArray[0] / 100); using (var ms = new MemoryStream()) { mapImage.Save(ms, encodingInfo.Encoder, encodingInfo.EncoderParams); var document = new Document(new Map(ms), settings) { Sessions = ed.Sessions }; if (document.Sessions.Count > 0) { document.ProjectionOrigin = document.Sessions[0].ProjectionOrigin; } document.FileName = fileName; document.FileFormat = QuickRouteFileFormat.Jpeg; document.Initialize(); mapAndBorderImage.Dispose(); mapImage.Dispose(); return(document); } }
public static QuickRouteJpegExtensionData FromImageExporter(ImageExporter exporter) { var data = new QuickRouteJpegExtensionData { Version = currentVersion, ImageCornerPositions = exporter.Document.GetImageCornersLongLat(exporter.ImageBounds, exporter.MapBounds, exporter.Properties.PercentualSize), MapCornerPositions = exporter.Document.GetMapCornersLongLat(), MapLocationAndSizeInPixels = exporter.MapBounds, Sessions = exporter.Sessions, PercentualSize = exporter.Properties.PercentualSize }; return(data); }
private static QuickRouteJpegExtensionData ParseStream(Stream stream) { var data = new QuickRouteJpegExtensionData { PercentualSize = 1 }; stream.Position = 0; var edReader = new BinaryReader(stream); while (stream.Position < stream.Length) { var tag = (Tags)edReader.ReadByte(); var tagLength = Convert.ToInt32(edReader.ReadUInt32()); switch (tag) { case Tags.Version: data.Version = edReader.ReadByte() + "." + edReader.ReadByte() + "." + edReader.ReadByte() + "." + edReader.ReadByte(); break; case Tags.MapCornerPositions: data.MapCornerPositions = new LongLat[4]; data.MapCornerPositions[0] = ReadLongLat(edReader); data.MapCornerPositions[1] = ReadLongLat(edReader); data.MapCornerPositions[2] = ReadLongLat(edReader); data.MapCornerPositions[3] = ReadLongLat(edReader); break; case Tags.ImageCornerPositions: data.ImageCornerPositions = new LongLat[4]; data.ImageCornerPositions[0] = ReadLongLat(edReader); data.ImageCornerPositions[1] = ReadLongLat(edReader); data.ImageCornerPositions[2] = ReadLongLat(edReader); data.ImageCornerPositions[3] = ReadLongLat(edReader); break; case Tags.MapLocationAndSizeInPixels: var rect = new Rectangle { X = edReader.ReadUInt16(), Y = edReader.ReadUInt16(), Width = edReader.ReadUInt16(), Height = edReader.ReadUInt16() }; data.MapLocationAndSizeInPixels = rect; break; case Tags.Sessions: data.Sessions = ReadSessions(edReader); break; default: edReader.BaseStream.Position += tagLength; break; } } return(data); }
private void CalculateImageAndTransformationMatrix(string fileName) { var zipFile = new Ionic.Zip.ZipFile(fileName); var mapSize = new Size(); Transformation = new Transformation(); // get entry for kml file and image file KmlDocument kmlDocument = null; foreach (var entry in zipFile) { if (entry.FileName == entry.LocalFileName && Path.GetExtension(entry.FileName) == ".kml") { using (var kmlStream = new MemoryStream()) { entry.Extract(kmlStream); kmlStream.Position = 0; kmlDocument = new KmlDocument(kmlStream); } break; } } if (kmlDocument != null) { // we have got a kml document, get map image file stream from it foreach (var entry in zipFile) { if (entry.FileName == kmlDocument.ImageFileName) { ImageStream = new MemoryStream(); entry.Extract(ImageStream); ImageStream.Position = 0; // check if image is QR jpeg var ed = QuickRouteJpegExtensionData.FromStream(ImageStream); if (ed != null) { // get transformation matrix from QR jpeg metadata Transformation = ed.Sessions.CalculateAverageTransformation(); ImageStream.Position = 0; return; } else { // it is not, use normal image bounds ImageStream.Position = 0; mapSize = Image.FromStream(ImageStream).Size; // need to get image object to get image size } ImageStream.Position = 0; break; } } } if (kmlDocument != null && ImageStream != null) { // finally, calculate the transformation Transformation = new Transformation(kmlDocument.LongLatBox, mapSize); } }
private static QuickRouteJpegExtensionData ParseStream(Stream stream) { var data = new QuickRouteJpegExtensionData {PercentualSize = 1}; stream.Position = 0; var edReader = new BinaryReader(stream); while (stream.Position < stream.Length) { var tag = (Tags)edReader.ReadByte(); var tagLength = Convert.ToInt32(edReader.ReadUInt32()); switch (tag) { case Tags.Version: data.Version = edReader.ReadByte() + "." + edReader.ReadByte() + "." + edReader.ReadByte() + "." + edReader.ReadByte(); break; case Tags.MapCornerPositions: data.MapCornerPositions = new LongLat[4]; data.MapCornerPositions[0] = ReadLongLat(edReader); data.MapCornerPositions[1] = ReadLongLat(edReader); data.MapCornerPositions[2] = ReadLongLat(edReader); data.MapCornerPositions[3] = ReadLongLat(edReader); break; case Tags.ImageCornerPositions: data.ImageCornerPositions = new LongLat[4]; data.ImageCornerPositions[0] = ReadLongLat(edReader); data.ImageCornerPositions[1] = ReadLongLat(edReader); data.ImageCornerPositions[2] = ReadLongLat(edReader); data.ImageCornerPositions[3] = ReadLongLat(edReader); break; case Tags.MapLocationAndSizeInPixels: var rect = new Rectangle { X = edReader.ReadUInt16(), Y = edReader.ReadUInt16(), Width = edReader.ReadUInt16(), Height = edReader.ReadUInt16() }; data.MapLocationAndSizeInPixels = rect; break; case Tags.Sessions: data.Sessions = ReadSessions(edReader); break; default: edReader.BaseStream.Position += tagLength; break; } } return data; }
public static QuickRouteJpegExtensionData FromQuickRouteDocument(Document document, SessionCollection sessions, Rectangle imageBounds, Rectangle mapBounds, double percentualSize) { var data = new QuickRouteJpegExtensionData { Version = currentVersion, ImageCornerPositions = document.GetImageCornersLongLat(imageBounds, mapBounds, percentualSize), MapCornerPositions = document.GetMapCornersLongLat(), MapLocationAndSizeInPixels = mapBounds, Sessions = sessions, PercentualSize = percentualSize }; return data; }
public static QuickRouteJpegExtensionData FromQuickRouteDocument(Document document) { var data = new QuickRouteJpegExtensionData {Version = currentVersion, Sessions = document.Sessions, PercentualSize = 1}; return data; }
public static QuickRouteJpegExtensionData FromImageExporter(ImageExporter exporter) { var data = new QuickRouteJpegExtensionData { Version = currentVersion, ImageCornerPositions = exporter.Document.GetImageCornersLongLat(exporter.ImageBounds, exporter.MapBounds, exporter.Properties.PercentualSize), MapCornerPositions = exporter.Document.GetMapCornersLongLat(), MapLocationAndSizeInPixels = exporter.MapBounds, Sessions = exporter.Sessions, PercentualSize = exporter.Properties.PercentualSize }; return data; }