I want to darken all the black text on a bitmap to filter the the bitmap and after research I found this:
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 });
}
My present challenge is applying it on the bitmap to darken the black texts.
Kindly assist me.
I was able to find an answer to my question using
https://stackoverflow.com/a/17887577/5220210
and
http://android.okhelp.cz/bitmap-set-contrast-and-brightness-android/
public static Bitmap darkenText(Bitmap bmp, float contrast)
{
ColorMatrix cm = new ColorMatrix();
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 });
Bitmap ret = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
Canvas canvas = new Canvas(ret);
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bmp, 0, 0, paint);
return ret;
}
Hope it helps someone.
Related
I am trying to develop applications for children. They will match objects with their own shadows. It's not grayscale. I want to make it completely black.
I tried the codes here. I tried changing some codes, but it wasn't successful.
Android : Converting color image to grayscale
public Bitmap onlyBlack(Bitmap bmpOriginal)
{
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpBlack = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpBlack);
Paint paint = new Paint();
float blck = 0;
float[] mat = new float[]{
blck, blck, blck, 0, 0,
blck, blck, blck, 0, 0,
blck, blck, blck, 0, 0,
0, 0, 0, 0, 0,};
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(mat);
paint.setColorFilter(filter);
c.drawBitmap(bmpOriginal, 0, 0, paint);
return bmpBlack;
}
I've done color in zero. But the colorless places in the picture are black.
How can I make a shadow appearance of a picture without a background?
I have the following code that reduces an image's contrast and by 50% and turns down the saturation to 0. Works flawlessly. However, I need to overlay a give colour over the resulting image, much like the PorterDuff.Mode.OVERLAY filter.
I've been fiddling with this for hours, didn't get me anywhere close to what I wanted.
float contrast = -0.5f;
float scale = contrast + 1.f;
float translate = (-.5f * scale + .5f) * 255.f;
float[] array = new float[]{
scale, 0, 0, 0, translate,
0, scale, 0, 0, translate,
0, 0, scale, 0, translate,
0, 0, 0, 1, 0};
ColorMatrix contrastMatrix = new ColorMatrix(array);
ColorMatrix saturationMatrix = new ColorMatrix();
saturationMatrix.setSaturation(0);
ColorMatrix matrix = new ColorMatrix();
matrix.setConcat(contrastMatrix, saturationMatrix);
// This is where I've been trying to overlay the colour by fiddling the colour matrix array.
// Didn't get the effect I wanted.
int primaryColor = context.getResources().getColor(R.color.primary500);
matrix.postConcat(new ColorMatrix(
new float[]{
Color.red(primaryColor)/255f, 0, 0, 0, 0,
0, Color.green(primaryColor)/255f, 0, 0, 0,
0, 0, Color.blue(primaryColor)/255f, 0, 0,
0, 0, 0, 1, 0}));
imageView.setColorFilter(filter);
The array I've used above give a much dark tone of blue. Can't seem the get the it right for some reason.
Any help would be much appreciated. Many thanks!
I'm trying to copy all the chars from an AWT font into a OpenGL array for LWJGL to use. Currently my code isn't working (blank textures). What I tried was to draw each char into a BufferedImage along with a transparent background, then copy it pixel-by-pixel onto a ByteBuffer for an OpenGL image. My question is, what is a better way around copying these chars? BufferedImage doesn't seem to be working. Is there another canvas type I can use for this?
So far this is the layout, but it doesn't work, I need a new way to draw onto the textures:
for (int i = 0; i <= 255; i++)
{
String letter = Character.toString((char)i);
BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setColor(new Color(255, 255, 255, 0));
g.drawRect(0, 0, size, size);
g.setColor(new Color(255, 255, 255));
g.setFont(font);
g.drawString(letter, 0, 0);
int id = i;
glTextures[id] = glGenTextures();
glBindTexture(GL_TEXTURE_2D, glTextures[id]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
ByteBuffer buf = ByteBuffer.allocateDirect(4 * 4 * size * size);
for (int x = 1; x <= size; x++)
{
for (int y = 1; y <= size; y++)
{
Color color = new Color(image.getRGB(x - 1, y - 1));
buf.putFloat((float)(1 / 255 * color.getRed()));
buf.putFloat((float)(1 / 255 * color.getGreen()));
buf.putFloat((float)(1 / 255 * color.getBlue()));
buf.putFloat((float)(1 / 255 * color.getAlpha()));
}
}
buf.flip();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_FLOAT, buf);
}
Im trying to create a vignette effect for my app. I've been searching a lot for help achieving this but was unable to find anything.
I recently found this tutorial.
And i tried to implement it in my app with this code:
public int[] drawBitmap(Bitmap originalBitmap){
Bitmap mask;
Paint paint = new Paint();
mask = convertToAlphaMask(BitmapFactory.decodeResource(context.getResources(), R.drawable.spot_mask));
Shader shader = createShader(mask);
paint.setShader(shader);
Bitmap tempBit = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(tempBit);
canvas.drawBitmap(originalBitmap, 0, 0,paint);
tempBit.getPixels(pixels, 0, tempBit.getWidth(), 0, 0, tempBit.getWidth(), tempBit.getHeight());
return pixels;
}
private static Bitmap convertToAlphaMask(Bitmap input) {
Bitmap a = Bitmap.createBitmap(input.getWidth(), input.getHeight(), Bitmap.Config.ALPHA_8);
Canvas c = new Canvas(a);
c.drawBitmap(input, 0.0f, 0.0f, null);
return a;
}
private static Shader createShader(Bitmap b) {
return new BitmapShader(b, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
}
But the effect of this looks like this:(the only change is at the top of the image)
http://postimg.org/image/rrivq28v1/
What have I done wrong?
Also, are there any other alternatives for applying a vignette effect on a bitmap?
Thanks!
After long time i found it
public Bitmap vignett(Bitmap bm, int p){
Bitmap image = Bitmap.createBitmap(bm.getWidth(),bm.getHeight(), Bitmap.Config.ARGB_8888);
int rad;
Canvas canvas = new Canvas(image);
canvas.drawBitmap(bm, 0, 0, new Paint());
if(bm.getWidth()<bm.getHeight()){
int o = (bm.getHeight()*2)/100;
rad = bm.getHeight() - o*p/3;
}else{
int o = (bm.getWidth()*2)/100;
rad = bm.getWidth() - o*p/3;
}
Rect rect = new Rect(0, 0, bm.getWidth(), bm.getHeight());
RectF rectf = new RectF(rect);
int[] colors = new int[] { 0, 0, Color.BLACK };
float[] pos = new float[] { 0.0f, 0.1f, 1.0f };
Shader linGradLR = new RadialGradient(rect.centerX(), rect.centerY(),rad, colors, pos, Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(linGradLR);
paint.setAntiAlias(true);
paint.setDither(true);
paint.setAlpha(255);
canvas.drawRect(rectf, paint);
return image;
}
here int p is standard value of seekbar from 1 to 100;
for intensity of effect u can set paint.setAlpha 0 to 255!!!
My first answer got deleted because i pointed to my solution via link. So I'll answer your question right here. The trick is to use 4 linear gradients. Applying them will give you a result that comes pretty close to a true Vignette effect. And it's freaking fast too ;) So here's part of my solution.
First you must create a canvas:
Canvas canvas = new Canvas(bitmapOut);
canvas.drawBitmap(mVignette.getBitmapIn(), 0, 0, null);
Then you define how far the effect should reach into your image:
int tenthLeftRight = (int)(width/5);
int tenthTopBottom = (int)(height/5);
Now you create your four shaders:
// Gradient left - right
Shader linGradLR = new LinearGradient(0, height/2, tenthLeftRight/2, height/2, Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP);
// Gradient top - bottom
Shader linGradTB = new LinearGradient(width/2, 0, width/2, tenthTopBottom, Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP);
// Gradient right - left
Shader linGradRL = new LinearGradient(width, height/2, (width-tenthLeftRight), height/2, Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP);
// Gradient bottom - top
Shader linGradBT = new LinearGradient(width/2, height, width/2, (height - tenthTopBottom), Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP);
Now all that's left to do is to draw on the canvas:
Paint paint = new Paint();
paint.setShader(linGradLR);
paint.setAntiAlias(true);
paint.setDither(true);
paint.setAlpha(125);
// Rect for Grad left - right
Rect rect = new Rect(0, 0, tenthLeftRight, height);
RectF rectf = new RectF(rect);
canvas.drawRect(rectf, paint);
// Rect for Grad top - bottom
paint.setShader(linGradTB);
rect = new Rect(0, 0, width, tenthTopBottom);
rectf = new RectF(rect);
canvas.drawRect(rectf, paint);
// Rect for Grad right - left
paint.setShader(linGradRL);
rect = new Rect(width, 0, width - tenthLeftRight, height);
rectf = new RectF(rect);
canvas.drawRect(rectf, paint);
// Rect for Grad bottom - top
paint.setShader(linGradBT);
rect = new Rect(0, height - tenthTopBottom, width, height);
rectf = new RectF(rect);
canvas.drawRect(rectf, paint);
Can anyone help me with a graphics issue I am having. This code does not apply the setPolyToPoly at all.. it does the Camera rotation, but not the polyToPoly transformation .. I dont understand why..
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
camera.translate(x, y, z);
camera.getMatrix(matrix);
camera.restore();
matrix.setPolyToPoly(sourceArr, 0, destArr, 0, sourceArr.length >> 1);
matrix.preTranslate(-0, -height);
matrix.postTranslate(0, height);
This sample does not fit your question entirely but might put you in the right direction. In this sample a matrix is applied to a bitmap that puts in a perspective. If I compare to your snippet, you do set the polytopoly but it is not applied to the camera.
Bitmap bitmap2 = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
Canvas canvas2 = new Canvas(bitmap2);
canvas2.drawColor(Color.WHITE);
Paint rectPaint2 = new Paint();
rectPaint2.setColor(Color.GREEN);
canvas2.drawRect(20, 20, 180, 180, rectPaint2);
Matrix matrix2 = new Matrix();
float deform2 = 20f;
float[] src2 = new float[] { 0, 0, bitmap2.getWidth(), 0, bitmap2.getWidth(), bitmap2.getHeight(), 0, bitmap2.getHeight() };
float[] dst2 = new float[] { 0, 0, bitmap2.getWidth() - deform2, deform2, bitmap2.getWidth() - deform2, bitmap2.getHeight() - deform2, 0, bitmap2.getHeight() };
matrix2.setPolyToPoly(src2, 0, dst2, 0, src2.length >> 1);
Bitmap bMatrix2= Bitmap.createBitmap(bitmap2, 0, 0, bitmap2.getWidth(), bitmap2.getHeight(), matrix2, true);
ImageView ivSecond = (ImageView) findViewById(R.id.ivSecond);
ivSecond.setImageBitmap(bMatrix2);