https://github.com/lirugo/screenDetector
кому интересно смотрите код тут
private final ImageReader.OnImageAvailableListener mOnImageAvailableListener = new
ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
Image image = reader.acquireLatestImage();
if(image != null) {
// Get buffer
ByteBuffer yBuff = image.getPlanes()[0].getBuffer();
ByteBuffer uBuff = image.getPlanes()[1].getBuffer();
ByteBuffer vBuff = image.getPlanes()[2].getBuffer();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] yByte = new byte[yBuff.remaining()];
byte[] uByte = new byte[uBuff.remaining()];
byte[] vByte = new byte[vBuff.remaining()];
yBuff.get(yByte);
uBuff.get(uByte);
vBuff.get(vByte);
// Create converting byte[] NV21
try {
outputStream.write(yByte);
for (int i=0; i < uByte.length; i++) {
outputStream.write(vByte[i]);
outputStream.write(uByte[i]);
}
} catch (Exception e) {
e.printStackTrace();
}
byte[] imageBytes = outputStream.toByteArray();
//Some var
int x,y,width,height,total;
width = image.getWidth();
height = image.getHeight();
total = width * height;
x = width / 2;
y = height / 2;
// Get YUV coordinates for x y position
int YCord = y * width + x;
int UCord = (y >> 1) * (width) + x + total + 1;
int VCord = (y >> 1) * (width) + x + total;
// Get YUV colors
int Y = (0xFF & imageBytes[YCord]) - 16;
int U = (0xFF & imageBytes[UCord]) - 128;
int V = (0xFF & imageBytes[VCord]) - 128;
Y = clamp(Y, 16, 235);
U = clamp(U, -112, 112);
V = clamp(V, -112, 112);
// Y = 81;
// U = -38;
// V = 112;
// if (Y < 0) Y += 255;
// if (U < 0) U += 127; else U -= 128;
// if (V < 0) V += 127; else V -= 128;
int r = (int) (Y + 1.370705 * V);
int g = (int) (Y - 0.698001 * V - 0.337633 * U);
int b = (int) (Y + 1.732446 * U);
//Clamp
r = clamp(r, 0, 255);
g = clamp(g, 0, 255);
b = clamp(b, 0, 255);
final YuvImage yuvImage = new YuvImage(outputStream.toByteArray(), ImageFormat.NV21, image.getWidth(),image.getHeight(), null);
ByteArrayOutputStream outBitmap = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0, 0,image.getWidth(), image.getHeight()), 100, outBitmap);
imageBytes = outBitmap.toByteArray();
try {
FileOutputStream fos = null;
fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory() + File.separator + Environment.DIRECTORY_DOWNLOADS,"MAGNET1" +
".jpg"));
fos.write(imageBytes);
fos.close();
String text = String.valueOf("( R" + r + ", G" + g + ", B" + b + " )");
Toast.makeText(getApplicationContext(), text , Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
image.close();
// try {
// TimeUnit.SECONDS.sleep(1);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
}
}
};