Inheritance: java.lang.Object
Example #1
0
        protected override ui.Image generatePeerImage()
        {
            if (element is MediaElement)
            {
                return(null);
            }

            int width  = getWidth();
            int height = getHeight();

            if (width <= 0 || height <= 0)
            {
                width  = getPreferredW();
                height = getPreferredH();
            }
            CodenameOneImage img = new CodenameOneImage();

            img.name = "PeerImage: " + element.ToString();
            IRandomAccessStream stream = new InMemoryRandomAccessStream();
            CanvasBitmap        cb     = null;

            SilverlightImplementation.dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
            {
                if (element is WebView)
                {
                    if (lastWebViewPeerImage != null)
                    {
                        cb = lastWebViewPeerImage;
                    }
                    else if (element.Visibility == Visibility.Visible && element.RenderSize.Width > 0 && element.RenderSize.Height > 0)
                    {
                        await((WebView)element).CapturePreviewToStreamAsync(stream);
                        await stream.FlushAsync();
                        stream.Seek(0);
                        cb = await CanvasBitmap.LoadAsync(SilverlightImplementation.screen, stream);
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
                    await renderTargetBitmap.RenderAsync(element);
                    byte[] buf = renderTargetBitmap.GetPixelsAsync().AsTask().ConfigureAwait(false).GetAwaiter().GetResult().ToArray();

                    cb = CanvasBitmap.CreateFromBytes(SilverlightImplementation.screen, buf, width, height,
                                                      SilverlightImplementation.pixelFormat, SilverlightImplementation.screen.Dpi);
                }
                img.image = new CanvasRenderTarget(SilverlightImplementation.screen, (int)cb.SizeInPixels.Width, (int)cb.SizeInPixels.Height, cb.Dpi);
                img.graphics.destination.drawImage(cb, 0, 0);
                img.graphics.destination.dispose();
            }).AsTask().GetAwaiter().GetResult();
            if (cb == null)
            {
                return(null);
            }
            return(ui.Image.createImage(img));
        }
Example #2
0
        public override void save(java.io.InputStream image, java.io.OutputStream response, string format, int width, int height, float quality)
        {
            CanvasBitmapFileFormat fileFormat = CanvasBitmapFileFormat.Png;

            if (format.Equals(FORMAT_JPEG))
            {
                fileFormat = CanvasBitmapFileFormat.Jpeg;
            }
            CodenameOneImage           img         = (CodenameOneImage)SilverlightImplementation.instance.createImage(image);
            CodenameOneImage           scaledImage = (CodenameOneImage)SilverlightImplementation.instance.scale(img, width, height);
            InMemoryRandomAccessStream ms          = new InMemoryRandomAccessStream();

            scaledImage.image.SaveAsync(ms, fileFormat, quality).AsTask().ConfigureAwait(false).GetAwaiter().GetResult();
            ms.Seek(0);
            byte[]     buf = new byte[ms.Size];
            DataReader dr  = new DataReader(ms);

            dr.LoadAsync((uint)ms.Size).AsTask().ConfigureAwait(false).GetAwaiter().GetResult();
            dr.ReadBytes(buf);
            response.write(buf);
        }
Example #3
0
        protected override void saveImage(ui.Image image, java.io.OutputStream response, string format, float quality)
        {
            CanvasBitmapFileFormat fileFormat = CanvasBitmapFileFormat.Png;

            if (format.Equals(FORMAT_JPEG))
            {
                fileFormat = CanvasBitmapFileFormat.Jpeg;
            }
            CodenameOneImage           img = (CodenameOneImage)image.getImage();
            CanvasBitmap               cb  = img.image;
            InMemoryRandomAccessStream ms  = new InMemoryRandomAccessStream();

            cb.SaveAsync(ms, fileFormat, quality).AsTask().ConfigureAwait(false).GetAwaiter().GetResult();;
            ms.Seek(0);
            byte[]     buf = new byte[ms.Size];
            DataReader dr  = new DataReader(ms);

            dr.LoadAsync((uint)ms.Size).AsTask().ConfigureAwait(false).GetAwaiter().GetResult();;
            dr.ReadBytes(buf);
            response.write(buf);
        }
Example #4
0
        public override object generatePeerImage()
        {
            int width = getWidth();
            int height = getHeight();
            if (width <= 0 || height <= 0)
            {
                width = getPreferredW();
                height = getPreferredH();
            }
            CodenameOneImage img = new CodenameOneImage();
            img.@this();
            img.name = "PeerImage: " + element.ToString();
            IRandomAccessStream stream = new InMemoryRandomAccessStream();
            CanvasBitmap cb = null;
            SilverlightImplementation.dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
            {
                if (element is WebView)
                {
                    await ((WebView)element).CapturePreviewToStreamAsync(stream);
                    await stream.FlushAsync();
                    stream.Seek(0);
                    cb = await CanvasBitmap.LoadAsync(SilverlightImplementation.screen, stream);
                }
                else
                {
                    RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
                    await renderTargetBitmap.RenderAsync(element);
                    byte[] buf = renderTargetBitmap.GetPixelsAsync().AsTask().ConfigureAwait(false).GetAwaiter().GetResult().ToArray();
                    cb = CanvasBitmap.CreateFromBytes(SilverlightImplementation.screen, buf, width, height,
                    SilverlightImplementation.pixelFormat, SilverlightImplementation.screen.Dpi);
                }
                img.image = new CanvasRenderTarget(SilverlightImplementation.screen, cb.SizeInPixels.Width, cb.SizeInPixels.Height, cb.Dpi);
                img.graphics.destination.drawImage(cb, 0, 0);
                img.graphics.destination.dispose();

            }).AsTask().GetAwaiter().GetResult();
            return ui.Image.createImage(img);
        }
        protected override com.codename1.ui.Image generatePeerImage()
        {
            int width  = getWidth();
            int height = getHeight();

            if (width <= 0 || height <= 0)
            {
                width  = getPreferredW();
                height = getPreferredH();
            }
            if (element.Parent == null)
            {
                if (peerImage != null)
                {
                    return((com.codename1.ui.Image)peerImage);
                }
                return(com.codename1.ui.Image.createImage(width, height));
            }
            CodenameOneImage img = new CodenameOneImage();

            //img.@this();
            img.name = "PeerImage: " + element.ToString();
            IRandomAccessStream stream = new InMemoryRandomAccessStream();
            CanvasBitmap        cb     = null;

            using (AutoResetEvent are = new AutoResetEvent(false))
            {
                SilverlightImplementation.dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    if (element is WebView)
                    {
                        try
                        {
                            Task.Delay(TimeSpan.FromTicks(4).Duration()).GetAwaiter().GetResult();
                            ((WebView)element).CapturePreviewToStreamAsync(stream).AsTask().GetAwaiter().GetResult();
                            stream.FlushAsync().AsTask().GetAwaiter().GetResult();
                            stream.Seek(0);
                            Task.Delay(TimeSpan.FromMilliseconds(10)).GetAwaiter().GetResult();
                            cb = CanvasBitmap.LoadAsync(SilverlightImplementation.screen, stream).AsTask().GetAwaiter().GetResult();
                        }
                        catch (Exception)
                        {
                            throw;
                        }
                    }
                    else
                    {
                        RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
                        renderTargetBitmap.RenderAsync(element).AsTask().ConfigureAwait(false).GetAwaiter().GetResult();
                        byte[] buf = renderTargetBitmap.GetPixelsAsync().AsTask().ConfigureAwait(false).GetAwaiter().GetResult().ToArray();
                        cb         = CanvasBitmap.CreateFromBytes(SilverlightImplementation.screen, buf, width, height, SilverlightImplementation.pixelFormat, SilverlightImplementation.screen.Dpi);
                    }
                    img.image = new CanvasRenderTarget(SilverlightImplementation.screen, cb.SizeInPixels.Width, cb.SizeInPixels.Height, cb.Dpi);
                    img.graphics.destination.drawImage(cb, 0, 0);
                    img.graphics.destination.dispose();
                    are.Set();
                }).AsTask().ConfigureAwait(false).GetAwaiter().GetResult();
                are.WaitOne();
            }
            return(com.codename1.ui.Image.createImage(img));

            // return com.codename1.ui.Image.createImage(width, height);
        }
        public override object generatePeerImage()
        {
            int width = getWidth();
            int height = getHeight();
            if (width <= 0 || height <= 0)
            {
                width = getPreferredW();
                height = getPreferredH();
            }
            if (element.Parent == null)
            {
                if (peerImage != null)
                {
                    return peerImage;
                }
                return com.codename1.ui.Image.createImage(width, height);
            }
            CodenameOneImage img = new CodenameOneImage();
            img.@this();
            img.name = "PeerImage: " + element.ToString();
            IRandomAccessStream stream = new InMemoryRandomAccessStream();
            CanvasBitmap cb = null;
            using (AutoResetEvent are = new AutoResetEvent(false))
            {
                SilverlightImplementation.dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
              {
                  if (element is WebView)
                  {
                      try
                      {
                          Task.Delay(TimeSpan.FromTicks(4).Duration()).GetAwaiter().GetResult();
                          ((WebView)element).CapturePreviewToStreamAsync(stream).AsTask().GetAwaiter().GetResult();
                          stream.FlushAsync().AsTask().GetAwaiter().GetResult();
                          stream.Seek(0);
                          Task.Delay(TimeSpan.FromMilliseconds(10)).GetAwaiter().GetResult();
                          cb = CanvasBitmap.LoadAsync(SilverlightImplementation.screen, stream).AsTask().GetAwaiter().GetResult();
                      }
                      catch (Exception)
                      {

                          throw;
                      }

                  }
                  else
                  {
                      RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
                      renderTargetBitmap.RenderAsync(element).AsTask().ConfigureAwait(false).GetAwaiter().GetResult();
                      byte[] buf = renderTargetBitmap.GetPixelsAsync().AsTask().ConfigureAwait(false).GetAwaiter().GetResult().ToArray();
                      cb = CanvasBitmap.CreateFromBytes(SilverlightImplementation.screen, buf, width, height, SilverlightImplementation.pixelFormat, SilverlightImplementation.screen.Dpi);
                  }
                  img.image = new CanvasRenderTarget(SilverlightImplementation.screen, cb.SizeInPixels.Width, cb.SizeInPixels.Height, cb.Dpi);
                  img.graphics.destination.drawImage(cb, 0, 0);
                  img.graphics.destination.dispose();
                  are.Set();
              }).AsTask().ConfigureAwait(false).GetAwaiter().GetResult();
                are.WaitOne();
            }
            return com.codename1.ui.Image.createImage(img);

            // return com.codename1.ui.Image.createImage(width, height);
        }
        public override object rotate(java.lang.Object img, int degrees) {
            CodenameOneImage cn = (CodenameOneImage)img;

            CanvasRenderTarget cr = new CanvasRenderTarget(screen, (float)cn.image.Size.Width, (float)cn.image.Size.Height, cn.image.Dpi);
            using (var ds = cr.CreateDrawingSession()) {
                float angle = (float)Math.PI * degrees / 180;
                ds.Transform = Matrix3x2.CreateRotation(angle, new Vector2(cr.SizeInPixels.Width / 2, cr.SizeInPixels.Height / 2));
                ds.DrawImage(cn.image);
                ds.Dispose();
            }
            CodenameOneImage ci = new CodenameOneImage();
            ci.@this();
            ci.image = cr;
            return ci;
        }
        public override global::System.Object createImage(global::org.xmlvm._nArrayAdapter<sbyte> n1, int n2, int n3) {
 //           var ticks = System.DateTime.Now.Ticks; //FA
            // I think Image Cache is not needed; first we load image and then we try to get it from cache?
            // other issue is that hasCode function is not working well for _nArrayAdapter
            //if (imageCache.ContainsKey(n1.hashCode())) {
            //    CodenameOneImage cached;
            //    imageCache.TryGetValue(n1.hashCode(), out cached);
            //    cached.lastAccess = System.DateTime.Now.Ticks;
            //    return cached;
            //}
            if (n1.Length == 0) {
                // workaround for empty images
                return createMutableImage(1, 1, 0xffffff);
            }

            CodenameOneImage ci = null;

            ///        using (AutoResetEvent are = new AutoResetEvent(false))
            ///        {
            ///            dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            ///            {
            IRandomAccessStream s;
            string contentType;
            if (n1.Length != n3 || n2 != 0) {
                // TODO
                byte[] imageArray = toByteArray(n1.getCSharpArray());
                contentType = ImageHelper.GetContentType(imageArray);
               
                s = new MemoryRandomAccessStream(imageArray); // new MemoryStream(imageArray).AsRandomAccessStream();
            }
            else {
                byte[] imageArray = toByteArray(n1.getCSharpArray());
                
                contentType = ImageHelper.GetContentType(imageArray);
                s = new MemoryRandomAccessStream(imageArray); // new MemoryStream(imageArray).AsRandomAccessStream();
            }
            try {
                CanvasBitmap canvasbitmap = CanvasBitmap.LoadAsync(screen, s).AsTask().ConfigureAwait(false).GetAwaiter().GetResult();
                CodenameOneImage cim = new CodenameOneImage();
                cim.@this();
                if (contentType.Equals("image/jpeg") || contentType.Equals("image/x-ms-bmp")) {
                    cim.opaque = true;
                }
                CanvasRenderTarget cr = new CanvasRenderTarget(screen, float.Parse(canvasbitmap.Size.Width.ToString()), float.Parse(canvasbitmap.Size.Height.ToString()), canvasbitmap.Dpi);
                cim.image = cr;
                cim.graphics.destination.drawImage(canvasbitmap, 0, 0);
                cim.graphics.destination.dispose();
                ci = cim;
                //dispatcher.RunAsync(CoreDispatcherPriority.Low, () => {
                //    imageCache.TryAdd(n1.hashCode(), ci);
                //    while (imageCache.Count > maxCacheSize) {
                //        int toRemove = imageCache.OrderBy(m => m.Value.lastAccess).First().Key;
                //        CodenameOneImage ignored;
                //        imageCache.TryRemove(toRemove, out ignored);
                //        //Debug.WriteLine("Image removed from cache " + toRemove);
                //    }
                //    //Debug.WriteLine("Image cached " + n1.hashCode());
                //}).AsTask();
                //Debug.WriteLine("Image created " + n1.hashCode());
            }
            catch (Exception) {
                Debug.WriteLine("\n Failed to create image " + n1.hashCode() + "\n Position: " + s.Position + "\n Size: " + s.Size);
            }
            ///              are.Set();
            ///          }).AsTask().ConfigureAwait(false).GetAwaiter().GetResult();
            ///          are.WaitOne();
            ///      }
            ///                  
//            ticks= (System.DateTime.Now.Ticks-ticks)/10; //FA
//            global::eu.forann.tools.LogF.eventNewItem(toJava("[createImage] ticks:" + ticks ));
            return ci;
        }
        public override object createMutableImage(int width, int height, int color) {
//            var ticks = System.DateTime.Now.Ticks; //FA

            CodenameOneImage ci = new CodenameOneImage();
            ci.@this();
            ci.mutable = true;
            ci.image = new CanvasRenderTarget(screen, screen.ConvertPixelsToDips(width), screen.ConvertPixelsToDips(height));
            ci.graphics.destination.setColor(color);
            ci.graphics.destination.setAlpha((color >> 24) & 0xff);
//            ticks= (System.DateTime.Now.Ticks-ticks)/10; //FA
//            global::eu.forann.tools.LogF.eventNewItem(toJava("[createMutableImage] ticks:" + ticks + "us w/h/c:" + width + "," + height + "/" + color));
 //           System.Diagnostics.Debug.WriteLine(System.DateTime.Now.ToString() + "[createMutableImage]ticks:" + ticks+"us w/h/c:"+width+","+height+"/"+color);
            //ci.graphics.destination.clear();
            return ci;
        }
        public override object scale(java.lang.Object sourceImage, int width, int height) {
            CodenameOneImage image = (CodenameOneImage)sourceImage;

            int srcWidth = image.getImageWidth();
            int srcHeight = image.getImageHeight();

            // no need to scale
            if (srcWidth == width && srcHeight == height) {
                return image;
            }

            CodenameOneImage ci = new CodenameOneImage();
            ci.@this();
            ci.opaque = image.opaque;
            if (width > 0 && height < 0) {
                height = srcHeight * width / srcWidth;
            }
            if (width < 0 && height > 0) {
                width = srcWidth * height / srcHeight;
            }

            CanvasRenderTarget cr = new CanvasRenderTarget(image.image.Device, image.image.ConvertPixelsToDips(width), image.image.ConvertPixelsToDips(height), image.image.Dpi);
            ci.image = cr;
            ci.graphics.destination.drawImage(image.image, 0, 0, width, height);
            ci.graphics.destination.dispose();
            return ci;
        }
 public override object scale(java.lang.Object n1, int n2, int n3)
 {
     CodenameOneImage ci = new CodenameOneImage();
     ci.@this();
     CodenameOneImage source = (CodenameOneImage)n1;
     ci.img = source.img;
     ci.width = n2;
     ci.height = n3;
     return ci;
 }
 public override global::System.Object createImage(global::org.xmlvm._nArrayAdapter<sbyte> n1, int n2, int n3)
 {
     CodenameOneImage ci = null;
     using (AutoResetEvent are = new AutoResetEvent(false))
     {
         System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
         {
             Stream s;
             if (n1.Length != n3 || n2 != 0)
             {
                 // TODO
                 s = new MemoryStream(toByteArray(n1.getCSharpArray()));
             }
             else
             {
                 s = new MemoryStream(toByteArray(n1.getCSharpArray()));
             }
             BitmapImage bitmapImage = new BitmapImage();
             CodenameOneImage cim = new CodenameOneImage();
             cim.@this();
             bitmapImage.SetSource(s);
             cim.img = bitmapImage;
             cim.width = (int)bitmapImage.PixelWidth;
             cim.height = (int)bitmapImage.PixelHeight;
             ci = cim;
             are.Set();
         });
         are.WaitOne();
     }
     return ci;
 }
 public override object createImage(_nArrayAdapter<int> n1, int n2, int n3)
 {
     CodenameOneImage ci = new CodenameOneImage();
     using (AutoResetEvent are = new AutoResetEvent(false))
     {
         System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
         {
             ci.@this();
             WriteableBitmap wb = new WriteableBitmap(n2, n3);
             int[] p = wb.Pixels;
             for (int iter = 0; iter < p.Length; iter++)
             {
                 p[iter] = n1.getCSharpArray()[iter];
             }
             ci.img = wb;
             ci.width = n2;
             ci.height = n3;
             are.Set();
         });
         are.WaitOne();
     }
     return ci;
 }
 public Image getExistingImage(CodenameOneImage source)
 {
     if (ll != null)
     {
         //log("Checking image existance for: " + source.name);
         foreach (UIElement uim in ll)
         {
             if (uim is Image)
             {
                 Image im = (Image)uim;
                 if (im.Source == source.img)
                 {
                     ll.Remove(im);
                     //log("Image found for: " + source.name);
                     return im;
                 }
             }
         }
     }
     if (source.imageCache != null && source.imageCache.Parent == null)
     {
         //log("Returning cached image for: " + source.name);
         return source.imageCache;
     }
     else
     {
         //log("Image not found for: " + source.name);
     }
     return null;
 }
        public override void drawRGB(java.lang.Object n1, _nArrayAdapter<int> rgb, int offset, int x, int y, int w, int h, bool n8)
        {
            CodenameOneImage ci = new CodenameOneImage();
            ci.@this();
            WriteableBitmap wb = new WriteableBitmap(w, h);
            int[] p = wb.Pixels;
            for (int iter = 0; iter < p.Length; iter++)
            {
                p[iter] = rgb.getCSharpArray()[iter];
            }
            ci.img = wb;
            ci.width = w;
            ci.height = h;

            NativeGraphics ng = (NativeGraphics)n1;
            ng.paint(new DrawImage(ng, pendingWipe, ci, x, y));
        }