Code Monkey home page Code Monkey logo

Comments (3)

CainKernel avatar CainKernel commented on July 24, 2024 1

you can use glReadPixels api to get bitmap from gpu and save it, such as:

        int width = getWidth();
        int height = getHeight();
        ByteBuffer buf = ByteBuffer.allocateDirect(width * height * 4);
        buf.order(ByteOrder.LITTLE_ENDIAN);
        GLES30.glReadPixels(0, 0, width, height,
                GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, buf);
        OpenGLUtils.checkGlError("glReadPixels");
        buf.rewind();

from caincamera.

CainKernel avatar CainKernel commented on July 24, 2024 1

in preview page,taking picture function is what you want。EglSurfaceBase.getCurrentFrame() is called from RenderThread.java to implement capture function and then using captureCallback.onCapture to save it,like this:

@Override
        public void onCapture(final ByteBuffer buffer, final int width, final int height) {
            mMainHandler.post(new Runnable() {
                @Override
                public void run() {
                    String filePath = PathConstraints.getImageCachePath(mActivity);
                    BitmapUtils.saveBitmap(filePath, buffer, width, height);
                    if (mPageListener != null) {
                        mPageListener.onOpenImageEditPage(filePath);
                    }
                }
            });
        }

so, if you want to read pixels in GLSurfaceView, you can call queueEvent() Api to implement it,just like this:

/**
     * 拍照
     */
    public void getCaptureFrame(final CaptureCallback captureCallback) {
        queueEvent(new Runnable() {
            @Override
            public void run() {
                int width = getWidth();
                int height = getHeight();
                ByteBuffer buf = ByteBuffer.allocateDirect(width * height * 4);
                buf.order(ByteOrder.LITTLE_ENDIAN);
                GLES30.glReadPixels(0, 0, width, height,
                        GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, buf);
                OpenGLUtils.checkGlError("glReadPixels");
                buf.rewind();
                if (captureCallback != null) {
                    captureCallback.onCapture(buf, width, height);
                }
            }
        });
    }

then, we save picture in fragment:

/**
     * 截屏回调
     */
    private GLImageSurfaceView.CaptureCallback mCaptureCallback = new GLImageSurfaceView.CaptureCallback() {
        @Override
        public void onCapture(final ByteBuffer buffer, final int width, final int height) {
            mMainHandler.post(new Runnable() {
                @Override
                public void run() {
                    String filePath = getDCIMImagePath(mActivity);
                    BitmapUtils.saveBitmap(filePath, buffer, width, height);
                    Log.d("hahaha", "run: " + filePath);
                }
            });
        }
    };

    /**
     * 获取图片缓存绝对路径
     * @param context
     * @return
     */
    private static String getDCIMImagePath(Context context) {
        String directoryPath;
        // 判断外部存储是否可用,如果不可用则使用内部存储路径
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            directoryPath =Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
        } else { // 使用内部存储缓存目录
            directoryPath = context.getCacheDir().getAbsolutePath();
        }
        String path = directoryPath + File.separator + Environment.DIRECTORY_PICTURES + File.separator + "CainCamera_" + System.currentTimeMillis() + ".jpeg";
        File file = new File(path);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        return path;
    }

from caincamera.

IhaYarre avatar IhaYarre commented on July 24, 2024

you can use glReadPixels api to get bitmap from gpu and save it, such as:

        int width = getWidth();
        int height = getHeight();
        ByteBuffer buf = ByteBuffer.allocateDirect(width * height * 4);
        buf.order(ByteOrder.LITTLE_ENDIAN);
        GLES30.glReadPixels(0, 0, width, height,
                GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, buf);
        OpenGLUtils.checkGlError("glReadPixels");
        buf.rewind();

do you have any tutorial for this ?

from caincamera.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.