使用GPUImage将应用的滤镜保存在相机预览中 [英] Save applied filter in camera preview with GPUImage

查看:119
本文介绍了使用GPUImage将应用的滤镜保存在相机预览中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GPUImage android库在相机预览上应用滤镜,并在拍照后使用滤镜保存图像.问题是,当我拿起象形文字时,我无法获得带有滤镜的图像. 我正在使用以下代码:

I am using GPUImage android library to apply filters on camera preview and save the image with filters applied after take a picture. The problem is that when I took the pictute, I can't get the image with the filters. I am using the following code:

@Override
public void onPictureTaken(byte[] data, Camera camera) {
    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

    mGPUImage.setImage(bitmap);
    bitmap = mGPUImage.getBitmapWithFilterApplied();
    saveImage(bitmap);
}

GPUImage库页面中的示例代码( https://github.com/Cyber​​Agent /android-gpuimage/#sample-code )说:

The sample code in GPUImage's library page (https://github.com/CyberAgent/android-gpuimage/#sample-code) says:

带预览:

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);

    Uri imageUri = ...;
    mGPUImage = new GPUImage(this);
    mGPUImage.setGLSurfaceView((GLSurfaceView) findViewById(R.id.surfaceView));
    mGPUImage.setImage(imageUri); // this loads image on the current thread, should be run in a thread ?? (can't understand this line)
    mGPUImage.setFilter(new GPUImageSepiaFilter());

    // Later when image should be saved saved:
    mGPUImage.saveToPictures("GPUImage", "ImageWithFilter.jpg", null);
}

即使在他们的示例中,我也无法使用过滤器保存图像. 请有人可以向我解释一下吗?

Even in their sample I can't save the image with filter. Please somebody could explain it to me?

推荐答案

使用此代码

    ///Call this method when you are ready to save image///
    //I am calling on click of save button//

    private void takePhoto() {
            releaseCamera();
            new AsyncTask<Void, Void, Void>() {
                Bitmap bitmap;

                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    try {
                        bitmap = gpuImageView.capture();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                protected Void doInBackground(Void... params) {
                    File dir = Util.getCameraDirectory(); // directory where you want to save image
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                    String filename = getString(R.string.app_name) + System.currentTimeMillis() + ".jpg";
                    File file = new File(dir, filename);
                    try {
                        FileOutputStream fileOutputStream = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fileOutputStream);

                        fileOutputStream.flush();
                        fileOutputStream.close();

                        Intent intent =
                                new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                        intent.setData(Uri.fromFile(file));
                        sendBroadcast(intent);
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }

                    return null;
                }

                @Override
                protected void onPostExecute(Void aVoid) {
                    super.onPostExecute(aVoid);
                    prepareCamera();
                    Toast.makeText(MyActivity.this, R.string.msg_after_save, Toast.LENGTH_SHORT).show();
                }
            }.execute();
        }

         private void prepareCamera() {
            camera = Camera.open(cameraId);
            Camera.Parameters parameters = camera.getParameters();
            Camera.Size size = getOptimalPreviewSize(camera.getParameters().getSupportedPreviewSizes(), getWindowManager().getDefaultDisplay().getWidth(), getWindowManager().getDefaultDisplay().getHeight());
            parameters.setPreviewSize(size.width, size.height);
            if (parameters.getSupportedFocusModes().contains(
                    Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
                parameters
                        .setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
            }
            parameters.setPreviewFormat(ImageFormat.NV21);
            camera.setParameters(parameters);

            Camera.CameraInfo info = new Camera.CameraInfo();
            Camera.getCameraInfo(cameraId, info);
            int orientation = getCameraDisplayOrientation(info);
            boolean flipHorizontal = info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT;
            gpuImageView.getGPUImage().setUpCamera(camera, orientation,
                    flipHorizontal, false);
        }

        private void releaseCamera() {
            if (camera != null) {
                camera.stopPreview();
                camera.setPreviewCallback(null);
                camera.release();
                camera = null;
            }
        }

让我知道您是否还有其他问题.

Let me know if you face any further issue.

此外,再次检查清单中的写许可权

Also, check again for write permission in manifest

这篇关于使用GPUImage将应用的滤镜保存在相机预览中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆