在 Android 中使用 colorMatrix 使黑色变暗
我目前有以下代码来获取位图对象,去除颜色,然后将其变成红色,这可以工作,但我需要图像的较暗元素变得更暗,目前就像有人在图像上放了红色胶片一样,这几乎是我想要的,但需要黑色更暗:
Bitmap sourceBitmap = BitmapFactory.decodeFile(imgPath);
float[] colorTransform = {
0, 1f, 0, 0, 0,
0, 0, 0f, 0, 0,
0, 0, 0, 0f, 0,
0, 0, 0, 1f, 0};
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0f); //Remove Colour
colorMatrix.set(colorTransform); //Apply the Red
ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
Paint paint = new Paint();
paint.setColorFilter(colorFilter);
Display display = getWindowManager().getDefaultDisplay();
Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, (int)(display.getHeight() * 0.15), display.getWidth(), (int)(display.getHeight() * 0.75));
image.setImageBitmap(resultBitmap);
Canvas canvas = new Canvas(resultBitmap);
canvas.drawBitmap(resultBitmap, 0, 0, paint);
I currently have the following code to get a Bitmap object, strip the color and then turn it red which works but I need the darker elements of the image to come through as darker, at the moment it's like somebody put a red film over the image, which is almost what I want but need the blacks to be darker:
Bitmap sourceBitmap = BitmapFactory.decodeFile(imgPath);
float[] colorTransform = {
0, 1f, 0, 0, 0,
0, 0, 0f, 0, 0,
0, 0, 0, 0f, 0,
0, 0, 0, 1f, 0};
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0f); //Remove Colour
colorMatrix.set(colorTransform); //Apply the Red
ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
Paint paint = new Paint();
paint.setColorFilter(colorFilter);
Display display = getWindowManager().getDefaultDisplay();
Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, (int)(display.getHeight() * 0.15), display.getWidth(), (int)(display.getHeight() * 0.75));
image.setImageBitmap(resultBitmap);
Canvas canvas = new Canvas(resultBitmap);
canvas.drawBitmap(resultBitmap, 0, 0, paint);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
颜色矩阵中前三个原始数据的最后一列会改变图像的亮度。它在 [-255...255] 之间变化。 -255 会给你黑色图像,255 会给你白色图像。
此方法可以改变对比度。较亮的物体会变得更亮,较暗的物体会变得更暗。您可以将亮度设置为所需的位置。对比度在 [-1...1] 之间变化。
Last column of first three raws in color matrix change brightness of image. Its changed between [-255...255]. -255 will gave you black image and 255 will make it white.
This method can change you contrast. Than bright object will become more bighter and dark more darker. Than you can set you brightness to requared poisition. Contrast changed between [-1...1].