get pixel color from bitmap android - android

I want to check the color of pixels in a section of a bitmap. I need to determine at what position the black pixels start. Here is my implementation.
Bitmap bmp = SavePixels(0, 0, screenWidth, screenHeight, gl);
for(int i = 0; i < 100; i++){
for(int x = 0; x < 50; x++){
//Log.i(EJ.TAG, i+","+x);
int pixel = bmp.getPixel(i,x);
if(pixel == Color.BLACK){
cordinate = i;
Log.i(EJ.TAG, i+","+x);
break;
}
}
}
Unfortunately the getPixel method always returns 0

This should find the first column (from left) containing a black pixel:
Bitmap bmp = SavePixels(0, 0, screenWidth, screenHeight, gl);
boolean found = false;
for(int x = 0; x < 50 && !found; x++){
for(int y = 0; y < 100 && !found; y++){
int pixel = bmp.getPixel(x, y);
if(pixel == Color.BLACK){
cordinate = x;
found=true;
}
}
}

Related

Save segmentation result of Selfie segmentation with ML Kit on Android as A Bitmap with transparent background

Save segmentation result of Selfie segmentation with ML Kit on Android as A Bitmap with transparent background
I am following this tutorial and code for Selfie segmentation
Here
I have referred this code from the tutorial
ByteBuffer mask = segmentationMask.getBuffer();
int maskWidth = segmentationMask.getWidth();
int maskHeight = segmentationMask.getHeight();
for (int y = 0; y < maskHeight; y++) {
for (int x = 0; x < maskWidth; x++) {
// Gets the confidence of the (x,y) pixel in the mask being in the foreground.
float foregroundConfidence = mask.getFloat();
}
}
Which generates a Mask
Then I have Referred the Sample app Which generates a purple background Mask
Here
using this code
#ColorInt
private int[] maskColorsFromByteBuffer(ByteBuffer byteBuffer) {
#ColorInt int[] colors = new int[maskWidth * maskHeight];
for (int i = 0; i < maskWidth * maskHeight; i++) {
float backgroundLikelihood = 1 - byteBuffer.getFloat();
if (backgroundLikelihood > 0.9) {
colors[i] = Color.argb(128, 255, 0, 255);
} else if (backgroundLikelihood > 0.2) {
// Linear interpolation to make sure when backgroundLikelihood is 0.2, the alpha is 0 and
// when backgroundLikelihood is 0.9, the alpha is 128.
// +0.5 to round the float value to the nearest int.
int alpha = (int) (182.9 * backgroundLikelihood - 36.6 + 0.5);
colors[i] = Color.argb(alpha, 255, 0, 255);
}
}
return colors;
}
Now I want to generate an Image with the original images detected mask and Overlay it on a Transparent Image and save that bitmap for this I am using this code
public Bitmap generateMaskBgImage(Bitmap image, Bitmap bg) {
//Bg is Transparent Png Image.
Bitmap bgBitmap = Bitmap.createBitmap(image.getWidth(), image.getHeight(), image.getConfig());
for (int y = 0; y < maskHeight; y++) {
for (int x = 0; x < maskWidth; x++) {
int bgConfidence = (int) ((1.0 - maskBuffer.getFloat()) * 255);
int bgPixel = bg.getPixel(x, y);
bgPixel = ColorUtils.setAlphaComponent(bgPixel, bgConfidence);
bgBitmap.setPixel(x, y, bgPixel);
}
}
maskBuffer.rewind();
return bitmapUtils.mergeBitmaps(image, bgBitmap);
}
However it generates an Image with the desired mask but with a Black back ground, How can we save that image with Transparent background.
You can try this (color1 is the color you set in the mask):
private Bitmap performBW(Bitmap originBitmap,Bitmap maskBitmap) {
Bitmap bmOut = Bitmap.createBitmap(originBitmap.getWidth(), originBitmap.getHeight(),
originBitmap.getConfig());
int w = originBitmap.getWidth();
int h = originBitmap.getHeight();
int[] colors = new int[w * h];
int[] colorsMask=new int[maskBitmap.getWidth() * maskBitmap.getHeight()];
originBitmap.getPixels(colors, 0, w, 0, 0, w, h);
maskBitmap.getPixels(colorsMask, 0, w, 0, 0, w, h);
int pos;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
pos = i * w + j;
if (colorsMask[pos] == color1) colors[pos]=Color.TRANSPARENT;
}
}
bmOut.setPixels(colors, 0, w, 0, 0, w, h);
return bmOut;
}

Android Zipper Animation for unlock screen

I am currently working on zip animation to unlock android mobile screen. Changing background images is a expensive task and have not a smooth effect. I want a smooth effect in it. Any help please? Thanks
Try this:
The smooth effect makes use of Convolution Matrix:
Some image effects are better to implement using Convolution Matrix
method like: Gaussian Blur, Sharpening, Embossing, Smooth…
Check That Link to know more about Convolution Matrix or Another one
To do Convolution Matrix
import android.graphics.Bitmap;
import android.graphics.Color;
public class ConvolutionMatrix
{
public static final int SIZE = 3;
public double[][] Matrix;
public double Factor = 1;
public double Offset = 1;
public ConvolutionMatrix(int size) {
Matrix = new double[size][size];
}
public void setAll(double value) {
for (int x = 0; x < SIZE; ++x) {
for (int y = 0; y < SIZE; ++y) {
Matrix[x][y] = value;
}
}
}
public void applyConfig(double[][] config) {
for(int x = 0; x < SIZE; ++x) {
for(int y = 0; y < SIZE; ++y) {
Matrix[x][y] = config[x][y];
}
}
}
public static Bitmap computeConvolution3x3(Bitmap src, ConvolutionMatrix matrix) {
int width = src.getWidth();
int height = src.getHeight();
Bitmap result = Bitmap.createBitmap(width, height, src.getConfig());
int A, R, G, B;
int sumR, sumG, sumB;
int[][] pixels = new int[SIZE][SIZE];
for(int y = 0; y < height - 2; ++y) {
for(int x = 0; x < width - 2; ++x) {
// get pixel matrix
for(int i = 0; i < SIZE; ++i) {
for(int j = 0; j < SIZE; ++j) {
pixels[i][j] = src.getPixel(x + i, y + j);
}
}
// get alpha of center pixel
A = Color.alpha(pixels[1][1]);
// init color sum
sumR = sumG = sumB = 0;
// get sum of RGB on matrix
for(int i = 0; i < SIZE; ++i) {
for(int j = 0; j < SIZE; ++j) {
sumR += (Color.red(pixels[i][j]) * matrix.Matrix[i][j]);
sumG += (Color.green(pixels[i][j]) * matrix.Matrix[i][j]);
sumB += (Color.blue(pixels[i][j]) * matrix.Matrix[i][j]);
}
}
// get final Red
R = (int)(sumR / matrix.Factor + matrix.Offset);
if(R < 0) { R = 0; }
else if(R > 255) { R = 255; }
// get final Green
G = (int)(sumG / matrix.Factor + matrix.Offset);
if(G < 0) { G = 0; }
else if(G > 255) { G = 255; }
// get final Blue
B = (int)(sumB / matrix.Factor + matrix.Offset);
if(B < 0) { B = 0; }
else if(B > 255) { B = 255; }
// apply new pixel
result.setPixel(x + 1, y + 1, Color.argb(A, R, G, B));
}
}
// final image
return result;
}
}
Then to do Smooth effect
public static Bitmap smooth(Bitmap src, double value) {
ConvolutionMatrix convMatrix = new ConvolutionMatrix(3);
convMatrix.setAll(1);
convMatrix.Matrix[1][1] = value;
convMatrix.Factor = value + 8;
convMatrix.Offset = 1;
return ConvolutionMatrix.computeConvolution3x3(src, convMatrix);
}
You can change values and get the smooth effect as you want.
That tutorial it's found HERE

Android Bitmap remove white margin

I've got a question regarding Bitmaps in Android: I 've got a Bitmap with white margins [size unknown] around. Is it possible to create a new Bitmap with all the white margins removed (rectangular shape)?
Bitmap bmp = Bitmap.createBitmap(width, bmpheigth, Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
canvas.setBitmap(bmp);
canvas.drawColor(Color.WHITE);
// draw here things!
It is asumed to be unknown where are things painted.
What is a good way to do that?
thanks!
Thanks #Maxim Efimov & #StackOverflowException
Just in Case Someone will need a snippet for this kind of problems:
this method returns a cut out smaller Bitmap with Margins removed. passing the pixels to a int-array first and then working with the array is a bit faster than the Bitmap.getPixel method
just call the method indicating Source Bitmap and Background color.
Bitmap bmp2 = removeMargins(bmp, Color.WHITE);
private static Bitmap removeMargins2(Bitmap bmp, int color) {
// TODO Auto-generated method stub
long dtMili = System.currentTimeMillis();
int MTop = 0, MBot = 0, MLeft = 0, MRight = 0;
boolean found1 = false, found2 = false;
int[] bmpIn = new int[bmp.getWidth() * bmp.getHeight()];
int[][] bmpInt = new int[bmp.getWidth()][bmp.getHeight()];
bmp.getPixels(bmpIn, 0, bmp.getWidth(), 0, 0, bmp.getWidth(),
bmp.getHeight());
for (int ii = 0, contX = 0, contY = 0; ii < bmpIn.length; ii++) {
bmpInt[contX][contY] = bmpIn[ii];
contX++;
if (contX >= bmp.getWidth()) {
contX = 0;
contY++;
if (contY >= bmp.getHeight()) {
break;
}
}
}
for (int hP = 0; hP < bmpInt[0].length && !found2; hP++) {
// looking for MTop
for (int wP = 0; wP < bmpInt.length && !found2; wP++) {
if (bmpInt[wP][hP] != color) {
Log.e("MTop 2", "Pixel found #" + hP);
MTop = hP;
found2 = true;
break;
}
}
}
found2 = false;
for (int hP = bmpInt[0].length - 1; hP >= 0 && !found2; hP--) {
// looking for MBot
for (int wP = 0; wP < bmpInt.length && !found2; wP++) {
if (bmpInt[wP][hP] != color) {
Log.e("MBot 2", "Pixel found #" + hP);
MBot = bmp.getHeight() - hP;
found2 = true;
break;
}
}
}
found2 = false;
for (int wP = 0; wP < bmpInt.length && !found2; wP++) {
// looking for MLeft
for (int hP = 0; hP < bmpInt[0].length && !found2; hP++) {
if (bmpInt[wP][hP] != color) {
Log.e("MLeft 2", "Pixel found #" + wP);
MLeft = wP;
found2 = true;
break;
}
}
}
found2 = false;
for (int wP = bmpInt.length - 1; wP >= 0 && !found2; wP--) {
// looking for MRight
for (int hP = 0; hP < bmpInt[0].length && !found2; hP++) {
if (bmpInt[wP][hP] != color) {
Log.e("MRight 2", "Pixel found #" + wP);
MRight = bmp.getWidth() - wP;
found2 = true;
break;
}
}
}
found2 = false;
int sizeY = bmp.getHeight() - MBot - MTop, sizeX = bmp.getWidth()
- MRight - MLeft;
Bitmap bmp2 = Bitmap.createBitmap(bmp, MLeft, MTop, sizeX, sizeY);
dtMili = (System.currentTimeMillis() - dtMili);
Log.e("Margin 2",
"Time needed " + dtMili + "mSec\nh:" + bmp.getWidth() + "w:"
+ bmp.getHeight() + "\narray x:" + bmpInt.length + "y:"
+ bmpInt[0].length);
return bmp2;
}
Use Bitmap.createBitmap(source, x, y, width, height) so knowing the white margin size you can do what you want.
My solution:
private Bitmap trim(Bitmap bitmap, int trimColor){
int minX = Integer.MAX_VALUE;
int maxX = 0;
int minY = Integer.MAX_VALUE;
int maxY = 0;
for(int x = 0; x < bitmap.getWidth(); x++){
for(int y = 0; y < bitmap.getHeight(); y++){
if(bitmap.getPixel(x, y) != trimColor){
if(x < minX){
minX = x;
}
if(x > maxX){
maxX = x;
}
if(y < minY){
minY = y;
}
if(y > maxY){
maxY = y;
}
}
}
}
return Bitmap.createBitmap(bitmap, minX, minY, maxX - minX + 1, maxY - minY + 1);
}
It isn't very fast, for 1280 x 576 px bitmap execution took 2965ms on Xiaomi Redmi 3S.
If it possible scale down image before triming:
private Bitmap scaleDown(Bitmap bitmap, float maxImageSize, boolean filter) {
float ratio = Math.min(maxImageSize / bitmap.getWidth(), maxImageSize / bitmap.getHeight());
int width = Math.round(ratio * bitmap.getWidth());
int height = Math.round(ratio * bitmap.getHeight());
return Bitmap.createScaledBitmap(bitmap, width, height, filter);
}
Late to the party, but this variation is a bit faster and perhaps easier to read:
public static Bitmap imageWithMargin(Bitmap bitmap, int color, int maxMargin) {
int maxTop = 0, maxBottom = 0, maxLeft = 0, maxRight = 0;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int[] bitmapArray = new int[width * height];
bitmap.getPixels(bitmapArray, 0, width, 0, 0, width, height);
// Find first non-color pixel from top of bitmap
searchTopMargin:
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (bitmapArray[width * y + x] != color) {
maxTop = y > maxMargin ? y - maxMargin : 0;
break searchTopMargin;
}
}
}
// Find first non-color pixel from bottom of bitmap
searchBottomMargin:
for (int y = height - 1; y >= 0; y--) {
for (int x = width - 1; x >= 0; x--) {
if (bitmapArray[width * y + x] != color) {
maxBottom = y < height - maxMargin ? y + maxMargin : height;
break searchBottomMargin;
}
}
}
// Find first non-color pixel from left of bitmap
searchLeftMargin:
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (bitmapArray[width * y + x] != color) {
maxLeft = x > maxMargin ? x - maxMargin : 0;
break searchLeftMargin;
}
}
}
// Find first non-color pixel from right of bitmap
searchRightMargin:
for (int x = width - 1; x >= 0; x--) {
for (int y = height - 1; y >= 0; y--) {
if (bitmapArray[width * y + x] != color) {
maxRight = x < width - maxMargin ? x + maxMargin : width;
break searchRightMargin;
}
}
}
return Bitmap.createBitmap(bitmap, maxLeft, maxTop, maxRight - maxLeft, maxBottom - maxTop);
}

Is it possible to remove transparent pixels from bitmap in android

In my application i am taking screenshot if the image doesn't fill imageView then transparent pixels are added to bitmap.Is it possible to remove transparent pixels from bitmap or take screenshot without transparent pixels.Thanks in advance.
This method is a lot faster:
static Bitmap trim(Bitmap source) {
int firstX = 0, firstY = 0;
int lastX = source.getWidth();
int lastY = source.getHeight();
int[] pixels = new int[source.getWidth() * source.getHeight()];
source.getPixels(pixels, 0, source.getWidth(), 0, 0, source.getWidth(), source.getHeight());
loop:
for (int x = 0; x < source.getWidth(); x++) {
for (int y = 0; y < source.getHeight(); y++) {
if (pixels[x + (y * source.getWidth())] != Color.TRANSPARENT) {
firstX = x;
break loop;
}
}
}
loop:
for (int y = 0; y < source.getHeight(); y++) {
for (int x = firstX; x < source.getWidth(); x++) {
if (pixels[x + (y * source.getWidth())] != Color.TRANSPARENT) {
firstY = y;
break loop;
}
}
}
loop:
for (int x = source.getWidth() - 1; x >= firstX; x--) {
for (int y = source.getHeight() - 1; y >= firstY; y--) {
if (pixels[x + (y * source.getWidth())] != Color.TRANSPARENT) {
lastX = x;
break loop;
}
}
}
loop:
for (int y = source.getHeight() - 1; y >= firstY; y--) {
for (int x = source.getWidth() - 1; x >= firstX; x--) {
if (pixels[x + (y * source.getWidth())] != Color.TRANSPARENT) {
lastY = y;
break loop;
}
}
}
return Bitmap.createBitmap(source, firstX, firstY, lastX - firstX, lastY - firstY);
}
I have done this way and it works great.
public static Bitmap createTrimmedBitmap(Bitmap bmp) {
int imgHeight = bmp.getHeight();
int imgWidth = bmp.getWidth();
int smallX=0,largeX=imgWidth,smallY=0,largeY=imgHeight;
int left=imgWidth,right=imgWidth,top=imgHeight,bottom=imgHeight;
for(int i=0;i<imgWidth;i++)
{
for(int j=0;j<imgHeight;j++)
{
if(bmp.getPixel(i, j) != Color.TRANSPARENT){
if((i-smallX)<left){
left=(i-smallX);
}
if((largeX-i)<right)
{
right=(largeX-i);
}
if((j-smallY)<top)
{
top=(j-smallY);
}
if((largeY-j)<bottom)
{
bottom=(largeY-j);
}
}
}
}
Log.d(TAG, "left:" + left + " right:" + right + " top:" + top + " bottom:" + bottom);
bmp=Bitmap.createBitmap(bmp,left,top,imgWidth-left-right, imgHeight-top-bottom);
return bmp;
}
Bitmap imageWithBG = Bitmap.createBitmap(image.getWidth(), image.getHeight(),image.getConfig()); // Create another image the same size
imageWithBG.eraseColor(Color.WHITE); // set its background to white, or whatever color you want
Canvas canvas = new Canvas(imageWithBG); // create a canvas to draw on the new image
canvas.drawBitmap(image, 0f, 0f, null); // draw old image on the background
image.recycle(); // clear out old image
To trim crop transparent borders of a image in Android you ca use
this arrange. Work faster because don't need read all pixels, it just slice the bitmap, more details: CropTrimTransparentImage
public Bitmap crop (Bitmap bitmap){
int height = bitmap.getHeight();
int width = bitmap.getWidth();
int[] empty = new int[width];
int[] buffer = new int[width];
Arrays.fill(empty,0);
int top = 0;
int left = 0;
int botton = height;
int right = width;
for (int y = 0; y < height; y++) {
bitmap.getPixels(buffer, 0, width, 0, y, width, 1);
if (!Arrays.equals(empty, buffer)) {
top = y;
break;
}
}
for (int y = height - 1; y > top; y--) {
bitmap.getPixels(buffer, 0, width, 0, y, width, 1);
if (!Arrays.equals(empty, buffer)) {
botton = y;
break;
}
}
int bufferSize = botton -top +1;
empty = new int[bufferSize];
buffer = new int[bufferSize];
Arrays.fill(empty,0);
for (int x = 0; x < width; x++) {
bitmap.getPixels(buffer, 0, 1, x, top + 1, 1, bufferSize);
if (!Arrays.equals(empty, buffer)) {
left = x;
break;
}
}
for (int x = width - 1; x > left; x--) {
bitmap.getPixels(buffer, 0, 1, x, top + 1, 1, bufferSize);
if (!Arrays.equals(empty, buffer)) {
right = x;
break;
}
}
Bitmap cropedBitmap = Bitmap.createBitmap(bitmap, left, top, right-left, botton-top);
return cropedBitmap;
}

java.lang.IllegalArgumentException when splitting bitmap in android

Can someone tell me why I am getting this error when splitting a bitmap.
Code:
public static List<Bitmap> ScambleImage(Bitmap image, int rows, int cols){
List<Bitmap> scambledImage = new ArrayList<Bitmap>();
int chunkWidth = image.getWidth(); //cols
int chunkHeight = image.getHeight(); //rows
int finalSize = chunkWidth/rows;
Bitmap bMapScaled = Bitmap.createScaledBitmap(image, chunkWidth, chunkHeight, true);
int yCoord = 0;//The y coordinate of the first pixel in source
for(int x = 0; x < rows; x++){
int xCoord = 0;//The x coordinate of the first pixel in source
for(int y = 0; y < cols; y++){
scambledImage.add(Bitmap.createBitmap(bMapScaled, xCoord, yCoord, finalSize, finalSize));
xCoord = finalSize + xCoord;
}
yCoord = finalSize + yCoord;//The y coordinate of the first pixel in source
}
return scambledImage;
}
rows = 6, and cols = 6;
image size = 648 x 484
this is the exception but don't know how to go about fixing:
java.lang.IllegalArgumentException: y + height must be <= bitmap.height()
Image I'm splitting
Thanks!
Your trying to grab sections of the original bitmap that don't exist.
Put a breakpoint at the line:
scambledImage.add(Bitmap.createBitmap(bMapScaled, xCoord, yCoord, finalSize, finalSize));
And you'll see it fails sometime after the first array iteration because each time your offsetting the start point of which "slice" of the bigmap you are grabbing by xCoord/yCoord.
My hunch says your calculation for finalSize is wrong, but I can only speculate since we don't know exactly what your trying to accomplish.
I have tried your code and changes a little bit and it works for me.
private ArrayList<Bitmap> splitImage(Bitmap bitmap, int rows, int cols){
int chunks = rows*cols;
int chunkHeight,chunkWidth;
ArrayList<Bitmap> splittedImages = null;
splittedImages = new ArrayList<Bitmap>(chunks);
chunkHeight = bitmap.getHeight()/rows;
chunkWidth = bitmap.getWidth()/cols;
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap,bitmap.getWidth(), bitmap.getHeight(), true);
int yCoord = 0;
for(int x=0; x<rows; x++){
int xCoord = 0;
for(int y=0; y<cols; y++){
splittedImages.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
xCoord += chunkWidth;
}
yCoord += chunkHeight;
}
return splittedImages;
}

Categories

Resources