Make Black darker using colorMatrix in Android
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 ColorMatrixColorFil开发者_开发知识库ter(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);
c = 2;//this will boost your contrast by 2x thus deepening the black (and lightning the white). I'm not sure why at 0 you have anything but black... maybe I don't understand the matrix as well as I think I do... I thought 1 (in place of my c) gets you the original colors.
Anyway... give that a whirl.
float[] colorTransform = {
c, 1f, 0, 0, 0,
0, c, 0f, 0, 0,
0, 0, c, 0f, 0,
0, 0, 0, 1f, 0};
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].
private static void setContrast(ColorMatrix cm, float contrast) {
float scale = contrast + 1.f;
float translate = (-.5f * scale + .5f) * 255.f;
cm.set(new float[] {
scale, 0, 0, 0, translate,
0, scale, 0, 0, translate,
0, 0, scale, 0, translate,
0, 0, 0, 1, 0 });
}
精彩评论