Exemplo n.º 1
0
        /**
         * Call from the GLThread to save a picture of the current frame.
         */
        public void SavePicture(IGL10 mGL)
        {
            var pixelData = new int[_mWidth * _mHeight];

            // Read the pixels from the current GL frame.
            IntBuffer buf = IntBuffer.Wrap(pixelData);
            IntBuffer ibt = IntBuffer.Allocate(_mWidth * _mHeight);

            buf.Position(0);
            mGL.GlReadPixels(0, 0, _mWidth, _mHeight, GLES20.GlRgba, GLES20.GlUnsignedByte, buf);

            // Create a file in the Pictures/HelloAR album.
            var file = new Java.IO.File($"{Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures)}/{TAG}", "Img" + DateTime.Now + ".png");

            // Make sure the directory exists
            if (!file.ParentFile.Exists())
            {
                file.ParentFile.Mkdirs();
            }

            // Convert the pixel data from RGBA to what Android wants, ARGB.
            var bitmapData = new int[pixelData.Length];

            for (var i = 0; i < _mHeight; i++)
            {
                for (var j = 0; j < _mWidth; j++)
                {
                    long p  = pixelData[i * _mWidth + j];
                    long b  = (p & 0x00ff0000) >> 16;
                    long r  = (p & 0x000000ff) << 16;
                    long ga = p & 0xff00ff00;
                    bitmapData[(_mHeight - i - 1) * _mWidth + j] = (int)(ga | r | b);
                }
            }

            // Convert upside down mirror-reversed image to right-side up normal
            // image.
            for (var i = 0; i < _mHeight; i++)
            {
                for (var j = 0; j < _mWidth; j++)
                {
                    ibt.Put((_mHeight - i - 1) * _mWidth + j, buf.Get(i * _mWidth + j));
                }
            }

            // Create a bitmap.
            Bitmap bmp = Bitmap.CreateBitmap(bitmapData, _mWidth, _mHeight, Bitmap.Config.Argb8888);

            bmp.CopyPixelsFromBuffer(ibt);

            // Write it to disk.
            var fs = new FileStream(file.Path, FileMode.OpenOrCreate);

            bmp.Compress(Bitmap.CompressFormat.Png, 100, fs);
            fs.Flush();
            fs.Close();
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Save filter bitmap from {@link ImageFilterView}
        /// </summary>
        /// <param name="glSurfaceView">surface view on which is image is drawn</param>
        /// <param name="gl">open gl source to read pixels from {@link GLSurfaceView}</param>
        /// <returns>save bitmap</returns>
        /// <OutOfMemoryError>error when system is out of memory to load and save bitmap</OutOfMemoryError>
        public static Bitmap CreateBitmapFromGlSurface(GLSurfaceView glSurfaceView, IGL10 gl)
        {
            try
            {
                //My Code Work <3
                var w = glSurfaceView.Width;
                var h = glSurfaceView.Height;

                var       ib  = IntBuffer.Allocate(w * h);
                IntBuffer ibt = IntBuffer.Allocate(w * h);

                try
                {
                    gl.GlReadPixels(0, 0, w, h, GL10.GlRgba, GL10.GlUnsignedByte, ib);

                    //Parallel.For(0, h, i =>
                    //{
                    //    for (var j = 0; j < w; j++)
                    //        ibt.Put((h - i - 1) * w + j, ib.Get(i * w + j));
                    //});

                    for (var i = 0; i < h; i++)
                    {
                        for (var j = 0; j < w; j++)
                        {
                            ibt.Put((h - i - 1) * w + j, ib.Get(i * w + j));
                        }
                    }

                    var mBitmap = Bitmap.CreateBitmap(w, h, Bitmap.Config.Argb8888);
                    mBitmap.CopyPixelsFromBuffer(ibt);
                    return(mBitmap);
                }
                catch (GLException e)
                {
                    Console.WriteLine(e);
                    return(null);
                }
                catch (OutOfMemoryError e)
                {
                    Console.WriteLine(e);
                    return(null);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(null);
            }
        }