-1

I'm taking a screenshot and transform it to Bitmap

Bitmap bitmap = null;
while (bitmap == null) { bitmap = BitmapFactory.decodeFile(path/filename.png); }

then i want to get pixel color

 int pixel = bitmap.getPixel(x, y);
 Log.i("Main", "color: " + "#" + Integer.toHexString(pixel));

and it works, but only when y<130. When y 140 and more i get only black color

2020-06-01 14:43:46.121 17451-17495/com. I/Main: color: #ff000000

screenshot resolution is 1920*1080. When i try load it in ImageView all is OK

Ксения
  • 67
  • 1
  • 8
  • How about x? What is OS version, device? – CoolMind Jun 01 '20 at 12:22
  • How do you get a screen-shot? See https://stackoverflow.com/questions/9245071/bitmap-getpixel-always-returning-black. – CoolMind Jun 01 '20 at 12:23
  • Did you see path/filename.png? What if change a picture to another from the Net? – CoolMind Jun 01 '20 at 12:24
  • x work good (when i change it change colors in log). OS Android 9. Yes i can see it. Width another pic all work. But why? – Ксения Jun 01 '20 at 12:29
  • screenshot get width adb shell input key event 120 – Ксения Jun 01 '20 at 12:35
  • Could you, please, post an example of `path/filename.png`? Strange that another picture has normal colors, while the screenshot has black beyond y > 140. There is a number of methods to take a screenhot: https://stackoverflow.com/questions/2661536/how-to-programmatically-take-a-screenshot-on-android. Even `input keyevent 120` was mentioned, but currently it is a bad variant. – CoolMind Jun 01 '20 at 12:39
  • /storage/emulated/0/Pictures/Screenshots/Screenshot_20200601-124514.png. And it is normal pic. I use input keyevent 120 because of it is not my View. I need get pixel color from screenshot not in my app – Ксения Jun 01 '20 at 12:50
  • Ксюша, do you get a screenshot from Service? Is a phone rooted? – CoolMind Jun 01 '20 at 12:54
  • You can also try to load it into `ImageView` and see what problems it has. – CoolMind Jun 01 '20 at 12:59
  • Have you tried: `Integer.toString(value, 16)` – Zbarcea Christian Jun 01 '20 at 13:38

1 Answers1

0

for me works to take screenshot like this

Process sh = Runtime.getRuntime().exec("su", null,null);
OutputStream os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + "/sdcard/img.png").getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor(); 
Ксения
  • 67
  • 1
  • 8