Exemplo n.º 1
0
        private static void SaveTempImage(CIImage fullimage, UIImage image, string outputFilename, int quality, bool rotate)
        {
            var imageData       = image.AsJPEG(quality);
            var dataProvider    = new CGDataProvider(imageData);
            var cgImageFromJpeg = CGImage.FromJPEG(dataProvider, null, false, CGColorRenderingIntent.Default);
            var imageWithExif   = new NSMutableData();
            var destination     = CGImageDestination.Create(imageWithExif, UTType.JPEG, 1);
            var cgImageMetadata = new CGMutableImageMetadata();
            var options         = new CGImageDestinationOptions();

            if (fullimage.Properties.DPIWidthF != null)
            {
                options.Dictionary[ImageIO.CGImageProperties.DPIWidth] =
                    new NSNumber((nfloat)fullimage.Properties.DPIWidthF);
            }
            if (fullimage.Properties.DPIWidthF != null)
            {
                options.Dictionary[ImageIO.CGImageProperties.DPIHeight] =
                    new NSNumber((nfloat)fullimage.Properties.DPIHeightF);
            }
            options.ExifDictionary = fullimage.Properties?.Exif ?? new CGImagePropertiesExif();
            options.TiffDictionary = fullimage.Properties?.Tiff ?? new CGImagePropertiesTiff();
            options.GpsDictionary  = fullimage.Properties?.Gps ?? new CGImagePropertiesGps();
            options.JfifDictionary = fullimage.Properties?.Jfif ?? new CGImagePropertiesJfif();
            options.IptcDictionary = fullimage.Properties?.Iptc ?? new CGImagePropertiesIptc();
            if (rotate)
            {
                options.Dictionary[ImageIO.CGImageProperties.Orientation] =
                    new NSString(UIImageOrientation.Up.ToString());
                var tiffDict = new CGImagePropertiesTiff();
                foreach (KeyValuePair <NSObject, NSObject> x in options.TiffDictionary.Dictionary)
                {
                    tiffDict.Dictionary.SetValueForKey(x.Value, x.Key as NSString);
                }
                tiffDict.Dictionary.SetValueForKey(new NSNumber(1), new NSString("Orientation"));
                options.TiffDictionary = tiffDict;
            }
            else
            {
                if (fullimage.Properties.Orientation != null)
                {
                    options.Dictionary[ImageIO.CGImageProperties.Orientation] =
                        new NSString(image.Orientation.ToString());
                }
            }
            destination.AddImageAndMetadata(cgImageFromJpeg, cgImageMetadata, options);
            var success = destination.Close();

            if (success)
            {
                imageWithExif.Save(outputFilename, true);
            }
            else
            {
                imageData.Save(outputFilename, true);
            }
        }
Exemplo n.º 2
0
        private string GeneratePdf()
        {
            NSMutableData printData = new NSMutableData();

            //create a PDF with empty rectangle, which will configure it for 8.5x11 inches

            var frame = new CGRect(0, 0, 0, 0);

            if (Orientation == PrintHelper.ORIENTATION.LANDSCAPE)
            {
                frame.Width  = 792f;
                frame.Height = 612f;
            }
            else
            {
                frame.Width  = 612f;
                frame.Height = 792f;
            }

            UIGraphics.BeginPDFContext(printData, frame, null);

            if (renderer != null)
            {
                for (int i = 0; i < renderer.NumberOfPages; i++)
                {
                    UIGraphics.BeginPDFPage();

                    renderer.DrawHeaderForPage(i, new CGRect());
                    renderer.DrawFooterForPage(i, new CGRect());
                    renderer.DrawContentForPage(i, new CGRect());
                    renderer.DrawPage(i, new CGRect());
                }
            }

            // complete a PDF page
            UIGraphics.EndPDFContent();

            var path    = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User) [0].Path;
            var pdfFile = System.IO.Path.Combine(path, "test.pdf");

            printData.Save(pdfFile, true);

            return(pdfFile);
        }
        public void RedrawDailyStockPDF(bool DocumentSigned)
        {
            // render created preview in PDF context
            NSMutableData pdfData = new NSMutableData();

            UIGraphics.BeginPDFContext(pdfData, GeneratedPDFView.Bounds, null);
            UIGraphics.BeginPDFPage();
            GeneratedPDFView.Layer.RenderInContext(UIGraphics.GetCurrentContext());
            UIGraphics.EndPDFContent();

            // save the rendered context to disk
            NSError err;
            string  pdfID = String.Format("{0}_{1}_{2}_UsedStock", MyConstants.EmployeeID, MyConstants.EmployeeName, DateTime.Now.Date.ToString("yyyy-MM-dd"));
            string  pdfFileName;

            if (DocumentSigned)
            {
                pdfFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), pdfID + "_Signed.pdf");
            }
            else
            {
                pdfFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), pdfID + "_Not_Signed.pdf");
            }
            pdfData.Save(pdfFileName, true, out err);

            if (err != null)
            {
                err.Dispose(); err = null;
            }
            if (pdfData != null)
            {
                pdfData.Dispose(); pdfData = null;
            }

            // set up UIWebView in signature view controller
            this.PDFView.MultipleTouchEnabled = true;
            this.PDFView.ScalesPageToFit      = true;
            this.PdfFileName = pdfFileName;

            if (DocumentSigned)
            {
                PDFView.LoadRequest(new NSUrlRequest(NSUrl.FromFilename(PdfFileName)));
            }
        }
Exemplo n.º 4
0
        private bool SaveImageWithMetadata(UIImage image, float quality, NSDictionary meta, string path)
        {
            var imageData          = image.AsJPEG(quality);
            var dataProvider       = new CGDataProvider(imageData);
            var cgImageFromJpeg    = CGImage.FromJPEG(dataProvider, null, false, CGColorRenderingIntent.Default);
            var imageWithExif      = new NSMutableData();
            var destination        = CGImageDestination.Create(imageWithExif, UTType.JPEG, 1);
            var cgImageMetadata    = new CGMutableImageMetadata();
            var destinationOptions = new CGImageDestinationOptions();

            if (meta.ContainsKey(ImageIO.CGImageProperties.ExifDictionary))
            {
                destinationOptions.ExifDictionary =
                    new CGImagePropertiesExif(meta[ImageIO.CGImageProperties.ExifDictionary] as NSDictionary);
            }
            if (meta.ContainsKey(ImageIO.CGImageProperties.TIFFDictionary))
            {
                destinationOptions.TiffDictionary = new CGImagePropertiesTiff(meta[ImageIO.CGImageProperties.TIFFDictionary] as NSDictionary);
            }
            if (meta.ContainsKey(ImageIO.CGImageProperties.GPSDictionary))
            {
                destinationOptions.GpsDictionary =
                    new CGImagePropertiesGps(meta[ImageIO.CGImageProperties.GPSDictionary] as NSDictionary);
            }
            if (meta.ContainsKey(ImageIO.CGImageProperties.JFIFDictionary))
            {
                destinationOptions.JfifDictionary =
                    new CGImagePropertiesJfif(meta[ImageIO.CGImageProperties.JFIFDictionary] as NSDictionary);
            }
            if (meta.ContainsKey(ImageIO.CGImageProperties.IPTCDictionary))
            {
                destinationOptions.IptcDictionary =
                    new CGImagePropertiesIptc(meta[ImageIO.CGImageProperties.IPTCDictionary] as NSDictionary);
            }
            destination.AddImageAndMetadata(cgImageFromJpeg, cgImageMetadata, destinationOptions);
            var success = destination.Close();

            if (success)
            {
                imageWithExif.Save(path, true);
            }
            return(success);
        }
        public void RedrawPrePlumbingPDF(bool ThereAndBack, bool DocumentSigned)
        {
            if (ThereAndBack)
            {
                _navWorkflow.PopToRootViewController(false);
            }
            // render created preview in PDF context
            NSMutableData pdfData = new NSMutableData();

            UIGraphics.BeginPDFContext(pdfData, _generatedPdfView.Bounds, null);
            UIGraphics.BeginPDFPage();
            _generatedPdfView.Layer.RenderInContext(UIGraphics.GetCurrentContext());
            UIGraphics.EndPDFContent();
            // save the rendered context to disk
            NSError err;
            string  pdfFileName;

            string pdfID = _navWorkflow._tabs._jobRunTable.CurrentCustomer.CustomerNumber.ToString() + "_PrePlumbingPDF";

            if (DocumentSigned)
            {
                pdfFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), pdfID + "_Signed.pdf");
            }
            else
            {
                pdfFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), pdfID + "_Not_Signed.pdf");
            }

            pdfData.Save(pdfFileName, true, out err);
            err = null; pdfData = null;

            // load the content into Signing view controller

            _navWorkflow._tabs.SignPre.PDFView.MultipleTouchEnabled = true;
            _navWorkflow._tabs.SignPre.PDFView.ScalesPageToFit      = true;
            _navWorkflow._tabs.SignPre.PDFView.LoadRequest(new NSUrlRequest(NSUrl.FromFilename(pdfFileName)));
            pdfPrePlumbingFileName = pdfFileName;
            // if (ThereAndBack) _navWorkflow.PushViewController (_navWorkflow._tabs._signView, true);
        }
        private bool SaveImageWithMetadata(UIImage image, float quality, NSDictionary meta, string path)
        {
            try
            {
                var finalQuality = quality;
                var imageData    = image.AsJPEG(finalQuality);
                //continue to move down quality , rare instances
                while (imageData == null && finalQuality > 0)
                {
                    finalQuality -= 0.05f;
                    imageData     = image.AsJPEG(finalQuality);
                }

                if (imageData == null)
                {
                    throw new NullReferenceException("Unable to convert image to jpeg, please ensure file exists or lower quality level");
                }

                var dataProvider       = new CGDataProvider(imageData);
                var cgImageFromJpeg    = CGImage.FromJPEG(dataProvider, null, false, CGColorRenderingIntent.Default);
                var imageWithExif      = new NSMutableData();
                var destination        = CGImageDestination.Create(imageWithExif, UTType.JPEG, 1);
                var cgImageMetadata    = new CGMutableImageMetadata();
                var destinationOptions = new CGImageDestinationOptions();

                if (meta.ContainsKey(ImageIO.CGImageProperties.Orientation))
                {
                    destinationOptions.Dictionary[ImageIO.CGImageProperties.Orientation] = meta[ImageIO.CGImageProperties.Orientation];
                }

                if (meta.ContainsKey(ImageIO.CGImageProperties.DPIWidth))
                {
                    destinationOptions.Dictionary[ImageIO.CGImageProperties.DPIWidth] = meta[ImageIO.CGImageProperties.DPIWidth];
                }

                if (meta.ContainsKey(ImageIO.CGImageProperties.DPIHeight))
                {
                    destinationOptions.Dictionary[ImageIO.CGImageProperties.DPIHeight] = meta[ImageIO.CGImageProperties.DPIHeight];
                }


                if (meta.ContainsKey(ImageIO.CGImageProperties.ExifDictionary))
                {
                    destinationOptions.ExifDictionary =
                        new CGImagePropertiesExif(meta[ImageIO.CGImageProperties.ExifDictionary] as NSDictionary);
                }


                if (meta.ContainsKey(ImageIO.CGImageProperties.TIFFDictionary))
                {
                    destinationOptions.TiffDictionary = new CGImagePropertiesTiff(meta[ImageIO.CGImageProperties.TIFFDictionary] as NSDictionary);
                }
                if (meta.ContainsKey(ImageIO.CGImageProperties.GPSDictionary))
                {
                    destinationOptions.GpsDictionary =
                        new CGImagePropertiesGps(meta[ImageIO.CGImageProperties.GPSDictionary] as NSDictionary);
                }
                if (meta.ContainsKey(ImageIO.CGImageProperties.JFIFDictionary))
                {
                    destinationOptions.JfifDictionary =
                        new CGImagePropertiesJfif(meta[ImageIO.CGImageProperties.JFIFDictionary] as NSDictionary);
                }
                if (meta.ContainsKey(ImageIO.CGImageProperties.IPTCDictionary))
                {
                    destinationOptions.IptcDictionary =
                        new CGImagePropertiesIptc(meta[ImageIO.CGImageProperties.IPTCDictionary] as NSDictionary);
                }
                destination.AddImageAndMetadata(cgImageFromJpeg, cgImageMetadata, destinationOptions);
                var success = destination.Close();
                if (success)
                {
                    imageWithExif.Save(path, true);
                }
                return(success);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Unable to save image with metadata: {ex}");
            }

            return(false);
        }
        internal static bool SaveImageWithMetadata(UIImage image, float quality, NSDictionary meta, string path, string pathExtension)
        {
            if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
            {
                return(SaveImageWithMetadataiOS13(image, quality, meta, path, pathExtension));
            }

            try
            {
                pathExtension = pathExtension.ToLowerInvariant();
                var finalQuality = quality;
                var imageData    = pathExtension == "jpg" ? image.AsJPEG(finalQuality) : image.AsPNG();

                //continue to move down quality , rare instances
                while (imageData == null && finalQuality > 0)
                {
                    finalQuality -= 0.05f;
                    imageData     = image.AsJPEG(finalQuality);
                }

                if (imageData == null)
                {
                    throw new NullReferenceException("Unable to convert image to jpeg, please ensure file exists or lower quality level");
                }

                var dataProvider       = new CGDataProvider(imageData);
                var cgImageFromJpeg    = CGImage.FromJPEG(dataProvider, null, false, CGColorRenderingIntent.Default);
                var imageWithExif      = new NSMutableData();
                var destination        = CGImageDestination.Create(imageWithExif, UTType.JPEG, 1);
                var cgImageMetadata    = new CGMutableImageMetadata();
                var destinationOptions = new CGImageDestinationOptions();

                if (meta.ContainsKey(ImageIO.CGImageProperties.Orientation))
                {
                    destinationOptions.Dictionary[ImageIO.CGImageProperties.Orientation] = meta[ImageIO.CGImageProperties.Orientation];
                }

                if (meta.ContainsKey(ImageIO.CGImageProperties.DPIWidth))
                {
                    destinationOptions.Dictionary[ImageIO.CGImageProperties.DPIWidth] = meta[ImageIO.CGImageProperties.DPIWidth];
                }

                if (meta.ContainsKey(ImageIO.CGImageProperties.DPIHeight))
                {
                    destinationOptions.Dictionary[ImageIO.CGImageProperties.DPIHeight] = meta[ImageIO.CGImageProperties.DPIHeight];
                }

                if (meta.ContainsKey(ImageIO.CGImageProperties.ExifDictionary))
                {
                    destinationOptions.ExifDictionary =
                        new CGImagePropertiesExif(meta[ImageIO.CGImageProperties.ExifDictionary] as NSDictionary);
                }

                if (meta.ContainsKey(ImageIO.CGImageProperties.TIFFDictionary))
                {
                    var existingTiffDict = meta[ImageIO.CGImageProperties.TIFFDictionary] as NSDictionary;
                    if (existingTiffDict != null)
                    {
                        var newTiffDict = new NSMutableDictionary();
                        newTiffDict.SetValuesForKeysWithDictionary(existingTiffDict);
                        newTiffDict.SetValueForKey(meta[ImageIO.CGImageProperties.Orientation], ImageIO.CGImageProperties.TIFFOrientation);
                        destinationOptions.TiffDictionary = new CGImagePropertiesTiff(newTiffDict);
                    }
                }
                if (meta.ContainsKey(ImageIO.CGImageProperties.GPSDictionary))
                {
                    destinationOptions.GpsDictionary =
                        new CGImagePropertiesGps(meta[ImageIO.CGImageProperties.GPSDictionary] as NSDictionary);
                }
                if (meta.ContainsKey(ImageIO.CGImageProperties.JFIFDictionary))
                {
                    destinationOptions.JfifDictionary =
                        new CGImagePropertiesJfif(meta[ImageIO.CGImageProperties.JFIFDictionary] as NSDictionary);
                }
                if (meta.ContainsKey(ImageIO.CGImageProperties.IPTCDictionary))
                {
                    destinationOptions.IptcDictionary =
                        new CGImagePropertiesIptc(meta[ImageIO.CGImageProperties.IPTCDictionary] as NSDictionary);
                }
                destination.AddImageAndMetadata(cgImageFromJpeg, cgImageMetadata, destinationOptions);
                var success = destination.Close();
                if (success)
                {
                    var saved = imageWithExif.Save(path, true, out NSError error);
                    if (error != null)
                    {
                        Debug.WriteLine($"Unable to save exif data: {error.ToString()}");
                    }

                    imageWithExif.Dispose();
                    imageWithExif = null;
                }

                return(success);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Unable to save image with metadata: {ex}");
            }

            return(false);
        }
Exemplo n.º 8
0
        private bool SaveImageWithMetadata(UIImage image, float quality, NSDictionary meta, string path)
        {
            try
            {

	            var imageData = GetImageWithQuality(quality, image);
	            var dataProvider = new CGDataProvider(imageData);
	            var cgImageFromJpeg = CGImage.FromJPEG(dataProvider, null, false, CGColorRenderingIntent.Default);
	            var imageWithExif = new NSMutableData();
	            var destination = CGImageDestination.Create(imageWithExif, UTType.JPEG, 1);
	            var cgImageMetadata = new CGMutableImageMetadata();
	            var destinationOptions = new CGImageDestinationOptions();

                if (meta.ContainsKey(ImageIO.CGImageProperties.DPIWidth))
					destinationOptions.Dictionary[ImageIO.CGImageProperties.DPIWidth] = meta[ImageIO.CGImageProperties.DPIWidth];

                if (meta.ContainsKey(ImageIO.CGImageProperties.DPIHeight))
                    destinationOptions.Dictionary[ImageIO.CGImageProperties.DPIHeight] = meta[ImageIO.CGImageProperties.DPIHeight];


				if (meta.ContainsKey(ImageIO.CGImageProperties.ExifDictionary))
	            {
					destinationOptions.ExifDictionary =
                                          new CGImagePropertiesExif(meta[ImageIO.CGImageProperties.ExifDictionary] as NSDictionary);
				}

                if (meta.ContainsKey(ImageIO.CGImageProperties.TIFFDictionary))
	            {
	                destinationOptions.TiffDictionary = new CGImagePropertiesTiff(meta[ImageIO.CGImageProperties.TIFFDictionary] as NSDictionary);
	                
                }
	            if (meta.ContainsKey(ImageIO.CGImageProperties.GPSDictionary))
	            {
	                destinationOptions.GpsDictionary =
	                    new CGImagePropertiesGps(meta[ImageIO.CGImageProperties.GPSDictionary] as NSDictionary);
	            }
	            if (meta.ContainsKey(ImageIO.CGImageProperties.JFIFDictionary))
	            {
	                destinationOptions.JfifDictionary =
	                    new CGImagePropertiesJfif(meta[ImageIO.CGImageProperties.JFIFDictionary] as NSDictionary);
	            }
	            if (meta.ContainsKey(ImageIO.CGImageProperties.IPTCDictionary))
	            {
	                destinationOptions.IptcDictionary =
	                    new CGImagePropertiesIptc(meta[ImageIO.CGImageProperties.IPTCDictionary] as NSDictionary);
	            }
				if (options.RotateImage)
				{
					if (meta.ContainsKey(ImageIO.CGImageProperties.Orientation))
						destinationOptions.Dictionary[ImageIO.CGImageProperties.Orientation] = new NSString(UIImageOrientation.Up.ToString());
					destinationOptions.TiffDictionary.Orientation = CoreImage.CIImageOrientation.TopLeft;
				}
				else
				{
					if (meta.ContainsKey(ImageIO.CGImageProperties.Orientation))
						destinationOptions.Dictionary[ImageIO.CGImageProperties.Orientation] = meta[ImageIO.CGImageProperties.Orientation];
				}
	            destination.AddImageAndMetadata(cgImageFromJpeg, cgImageMetadata, destinationOptions);
	            var success = destination.Close();
	            if (success)
	            {
	                imageWithExif.Save(path, true);
	            }
                return success;

			}
			catch (Exception ex)
			{
				Console.WriteLine($"Unable to save image with metadata: {ex}");
			}

            return false;
        }