如何在Android上的setColorFilter之后保存Drawable

时间:2016-08-29 05:57:53

标签: android drawable colormatrixfilter

要将颜色过滤器应用于来自takePicure()方法的字节数组,我将其转换为Drawable。在调用Drawable.setColorFilter()后,我将其保存为图像文件。但是,滤色器不适用于图像文件。在这种情况下,如何保存可绘制的应用彩色滤镜。这是我的代码。

Camera.PictureCallback mPicture = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] bytes, Camera camera) {
            mView.mRenderer.restartPreview();
            String storageDir = Environment.getExternalStorageDirectory().getAbsolutePath()
                    + "/" + Environment.DIRECTORY_DCIM + "/Camera";
            File dir = new File(storageDir);

            if (!dir.exists()) dir.mkdir();

            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String path = storageDir + "/IMG_" + timeStamp + ".jpg";

            File file = new File(path);
            try {
                ColorMatrix matrix = new ColorMatrix(new float[] {
                        1, 0, 0, 0, mView.mRenderer.mTest,
                        0, 1, 0, 0, mView.mRenderer.mTest,
                        0, 0, 1, 0, mView.mRenderer.mTest,
                        0, 0, 0, 1, 0
                });

                Drawable image = new BitmapDrawable(BitmapFactory.decodeByteArray(bytes, 0, bytes.length));
                image.setColorFilter(new ColorMatrixColorFilter(matrix));
                Bitmap bitmap = ((BitmapDrawable)image).getBitmap();
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                byte[] bytedata = stream.toByteArray();

                FileOutputStream fos = new FileOutputStream(file);
                fos.write(bytedata);
                fos.flush();
                fos.close();

                Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                Uri uri = Uri.parse("file://" + path);
                intent.setData(uri);
                sendBroadcast(intent);
            }
            catch (Exception e) {
                Log.e("CheckLog", e.getMessage());
                return;
            }
        }
};

mView.mRenderer.mTest是由SeekBar控制的变量。提前谢谢。

1 个答案:

答案 0 :(得分:5)

使用画布来应用矩阵。

.getAttribute('data-template')