示例#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();
        }
        public DistortionRenderer()
        {
            mTextureId             = -1;
            mRenderbufferId        = -1;
            mFramebufferId         = -1;
            mOriginalFramebufferId = IntBuffer.Allocate(1);
            mCullFaceEnabled       = IntBuffer.Allocate(1);
            mScissorTestEnabled    = IntBuffer.Allocate(1);
            mViewport = IntBuffer.Allocate(4);

            mResolutionScale = 1.0F;
        }
示例#3
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);
            }
        }
示例#4
0
        public static void LoadDict(Context context)
        {
            try
            {
                bool resaveEntries = false;
                dictParts   = new List <byte[]>();
                dictIndexes = new List <int>();

                File dictFd = new File(context.FilesDir, "dict.db");
                if (!dictFd.Exists())
                { // || dictFd.length() != 4961308) {
                    System.Console.WriteLine("DOES NOT EXIST!!!!!");
                    CopyFile(context, "dict.db");
                    dictFd        = new File(context.FilesDir, "dict.db");
                    resaveEntries = true;
                }
                dictFile = new RandomAccessFile(dictFd, "r");

                File idxFd = new File(context.FilesDir, "idx.db");
                if (!idxFd.Exists())
                { // || idxFd.length() != 3145553) {
                    CopyFile(context, "idx.db");
                    idxFd         = new File(context.FilesDir, "idx.db");
                    resaveEntries = true;
                }
                FileInputStream idxBuf = new FileInputStream(idxFd);

                if (!new File(context.FilesDir, "entries.bin").Exists() || !new File(context.FilesDir, "parts.bin").Exists())
                {
                    resaveEntries = true;
                }

                entries = IntBuffer.Allocate(1649830);

                int index = 0;
                //System.Console.WriteLine("LoadDict STEP 1");

                if (idxBuf != null)
                {
                    int    readLen, offset = 0, partLen = 200000;
                    byte[] dictPart  = new byte[partLen];
                    int    totalRead = 0;
                    int    totalLen  = (int)idxFd.Length();

                    while (totalRead < totalLen && (readLen = idxBuf.Read(dictPart, offset, dictPart.Length - offset)) > 0)
                    {
                        //System.Console.WriteLine("LoadDict \ntotalRead = " + totalRead + "\ntotalLen = " + totalLen + "\nreadLen = " + readLen + "\nidxBuf.Read = " + idxBuf.Read(dictPart, offset, dictPart.Length - offset));

                        totalRead += readLen;
                        int j = offset + readLen - 1;

                        byte[] newDictPart = null;

                        if (readLen == partLen - offset)
                        {
                            //System.Console.WriteLine("LoadDict STEP 4.1 " + dictPart[j] + " :: j => " + j);

                            while (dictPart[j] > 0)
                            {
                                //System.Console.WriteLine("j = " + j + "\ndictPart[j] = " + dictPart[j]);

                                j--;
                            }
                            //System.Console.WriteLine("LoadDict STEP 4.2");

                            while (dictPart[j] < 0)
                            {
                                System.Console.WriteLine("j = " + j);

                                j--;
                            }
                            //System.Console.WriteLine("LoadDict STEP 4.3");

                            offset = partLen - j - 1;
                            //System.Console.WriteLine("LoadDict STEP 4.4");

                            newDictPart = new byte[Math.Min(totalLen - totalRead + offset, partLen)];
                            //System.Console.WriteLine("LoadDict STEP 4.5");

                            Java.Lang.JavaSystem.Arraycopy(dictPart, j + 1, newDictPart, 0, offset);
                            //Array.Copy(dictPart, j + 1, newDictPart, 0, offset);
                        }
                        else
                        {
                            offset = 0;
                        }
                        //System.Console.WriteLine("LoadDict STEP 5");

                        if (resaveEntries)
                        {
                            dictIndexes.Add(index);
                            //System.Console.WriteLine("LoadDict STEP 6");

                            int i = 0;
                            while (i <= j)
                            {
                                entries.Put(index++, i);

                                while (i <= j && dictPart[i] < 0)
                                {
                                    i++;
                                }
                                while (i <= j && dictPart[i] >= 0)
                                {
                                    i++;
                                }
                            }
                        }
                        //System.Console.WriteLine("LoadDict STEP 7");

                        dictParts.Add(dictPart);
                        dictPart = newDictPart;
                        //System.Console.WriteLine("LoadDict STEP 8");
                    }
                    idxBuf.Close();
                }

                if (resaveEntries)
                {
                    //System.Console.WriteLine("LoadDict STEP 9");

                    DataOutputStream entriesOut = null, partsOut = null;
                    //System.Console.WriteLine("LoadDict STEP 10");

                    entriesOut = new DataOutputStream(context.OpenFileOutput("entries.bin", FileCreationMode.Private));
                    int count = entries.Capacity();
                    for (int i = 0; i < count; i++)
                    {
                        entriesOut.WriteInt(entries.Get(i));
                    }
                    //System.Console.WriteLine("LoadDict STEP 11");


                    partsOut = new DataOutputStream(context.OpenFileOutput("parts.bin", FileCreationMode.Private));
                    foreach (int i in dictIndexes)
                    {
                        partsOut.WriteInt(i);
                    }
                    //System.Console.WriteLine("LoadDict STEP 12");

                    if (entriesOut != null)
                    {
                        entriesOut.Flush();
                        entriesOut.Close();
                    }
                    if (partsOut != null)
                    {
                        partsOut.Flush();
                        partsOut.Close();
                    }
                }
                else
                {
                    //System.Console.WriteLine("LoadDict NOW RESAVING ENTRIES");

                    string       documentpath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                    Java.IO.File sdpath       = global::Android.OS.Environment.GetExternalStoragePublicDirectory(global::Android.OS.Environment.DirectoryDownloads);

                    File entriesDB = new File(documentpath, "entries.bin");
                    File partsDB   = new File(documentpath, "parts.bin");

                    FileInputStream entriesIn = null, partsIn = null;

                    //entriesIn = context.OpenFileInput("entries.bin");
                    entriesIn = new FileInputStream(entriesDB);
                    //entriesIn = new FileInputStream(new File("entries.bin"));
                    FileChannel file = entriesIn.Channel;
                    ByteBuffer  bb   = ByteBuffer.Allocate(4 * 1649830);
                    file.Read(bb);
                    bb.Rewind();
                    entries = bb.AsIntBuffer();
                    file.Close();

                    partsIn = new FileInputStream(partsDB);
                    //partsIn = new FileInputStream(new File("parts.bin"));
                    //partsIn = (context.OpenFileInput("parts.bin");
                    file = partsIn.Channel;
                    bb   = ByteBuffer.Allocate((int)file.Size());
                    file.Read(bb);
                    bb.Rewind();
                    IntBuffer ib    = bb.AsIntBuffer();
                    int       count = ib.Capacity();
                    //System.Console.WriteLine("LoadDict STEP 99 " + count);

                    for (int i = 0; i < count; i++)
                    {
                        dictIndexes.Add(ib.Get(i));
                    }
                    file.Close();

                    if (entriesIn != null)
                    {
                        entriesIn.Close();
                    }
                    if (partsIn != null)
                    {
                        partsIn.Close();
                    }
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Dict LoadDict ERROR => " + e.Message);

                Log.Equals("chinesreader", e.Message);
            }

            byteBuffer = new byte[1090];

            sharedPrefs = PreferenceManager.GetDefaultSharedPreferences(context);
        }