I'm trying to capture a view as a screenshot and print it via bluetooth using (Xprinter), but everytime i try to convert the bitmap to greyScale it produces an empty bitmap.
here is my captureScreenshot method, it seems like well..
public static Bitmap captureScreenshot(View view) {
Bitmap bitmap = null;
String fileName;
try {// create bitmap screen capture
view.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Bitmap.CompressFormat compressionFormat;
int filename = view.getId();
if (filename == -1) {
filename = 1010; //something
}
//Little bit of compression. JPG if too big, PNG if all good
if (getBitMapSize(bitmap) > 750000) {
compressionFormat = Bitmap.CompressFormat.JPEG;
fileName = filename + ".jpg";
Log.e("checkResized", "JPEG");
} else {
compressionFormat = Bitmap.CompressFormat.PNG;
fileName = filename + ".png";
Log.e("checkResized", "PNG");
}
bitmap.compress(compressionFormat, 40, outputStream);
} catch (Exception e) {
Log.e("checkE",e.toString());
//All kinds of exceptions
}
return bitmap;
}
and here the converting to grayscale method, which i think the problem is here..
public Bitmap convertGreyImg(Bitmap img) {
int width = img.getWidth();
int height = img.getHeight();
Log.e("printError", width + " ss "+ height);
int[] pixels = new int[width * height];
img.getPixels(pixels, 0, width, 0, 0, width, height);
//The arithmetic average of a grayscale image; a threshold
double redSum=0,greenSum=0,blueSun=0;
double total=width*height;
Log.e("printError", total + " ss ");
for(int i = 0; i < height; i++)
{
for(int j=0; j<width; j++) {
int grey = pixels[width * i + j];
int red = ((grey & 0x00FF0000 ) >> 16);
int green = ((grey & 0x0000FF00) >> 8);
int blue = (grey & 0x000000FF);
redSum+=red;
greenSum+=green;
blueSun+=blue;
}
}
int m=(int) (redSum/total);
//Conversion monochrome diagram
for(int i = 0; i < height; i++) {
for(int j = 0; j < width; j++) {
int grey = pixels[width * i + j];
int alpha1 = 0xFF << 24;
int red = ((grey & 0x00FF0000 ) >> 16);
int green = ((grey & 0x0000FF00) >> 8);
int blue = (grey & 0x000000FF);
if (red>=m) {
red=green=blue=255;
} else {
red=green=blue=0;
}
grey = alpha1 | (red << 16) | (green << 8) | blue;
pixels[width*i+j]=grey;
}
}
Bitmap mBitmap=Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
mBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
invoicePic.setImageBitmap(mBitmap);
return mBitmap;
}
and when i try to print a drawable image , it prints very well! please help me to find the error. thanks.
User contributions licensed under CC BY-SA 3.0