Android 上的 setColorFilter 后如何保存 Drawable

How to save Drawable after setColorFilter on Android

为了对来自 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控制的变量。提前致谢。

使用 canvas 应用矩阵。

Bitmap bitmap = Bitmap.createBitmap(original.getWidth(), 
original.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);

Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(
matrix));
canvas.drawBitmap(original, 0, 0, paint);

return bitmap;