adb shell input https://stackoverflow.com/questions/7789826/adb-shell-input-events adb shell input keyevent 82 https://stackoverflow.com/questions/13850192/how-to-lock-android-screen-via-adb glob.glob() https://stackoverflow.com/questions/39327032/how-to-get-the-latest-file-in-a-folder-using-python os.startfile() https://stackoverflow.com/questions/15054434/how-can-i-open-files-in-external-programs-in-python repr() https://bytes.com/topic/python/answers/670554-how-print-raw-string-data-variable re.search().group() https://stackoverflow.com/questions/4666973/how-to-extract-a-substring-from-inside-a-string-in-python subprocess.check_output() https://stackoverflow.com/questions/4514751/pipe-subprocess-standard-output-to-a-variable adb_android https://github.com/vmalyi/adb_android/blob/master/adb_android/adb_android.py adb shell pull https://stackoverflow.com/questions/39194080/how-to-pull-photos-with-adb-from-local-storage-android-device adb shell am start https://coderwall.com/p/3-tgjg/take-a-photo-via-adb
Last active
June 24, 2024 22:35
-
-
Save me7/12ad7c823a1b5d1a061dceb4fcd72b87 to your computer and use it in GitHub Desktop.
use adb to capture image and transfer to PC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# check device present: adb devices -> 5203d550fe3ea3ab | |
from time import clock, sleep | |
from subprocess import check_output, call | |
import re | |
#call this to capture image | |
def capture(): | |
clock() | |
screen_on() | |
clear_camera_folder() | |
open_camera() | |
press_camera_button() | |
transfer_img('c:/temp') | |
screen_off() | |
def screen_on(): | |
# check screen on/off state: adb shell dumpsys power | grep state --> Display Power: state=OFF\r\r or ON\r\r | |
if DEBUG: print 'screen_on'.ljust(20), clock() | |
r = check_output(['adb','shell','dumpsys','power']) | |
try: | |
state = re.search('Display Power: state=(.*)',r).group(1) | |
except AttributeError: | |
state = 'Unknown' | |
## DEBUG print 'state is',repr(state) | |
# if OFF turn it on: adb shell input keyevent = POWER | |
if state.strip() == 'OFF': | |
r = check_output(['adb','shell','input','keyevent = POWER']) | |
if DEBUG: print r.strip() | |
def open_camera(): | |
# check if camera is the top most: adb shell dumpsys activity activities | grep mFocusedActivity --> mFocusedActivity: ActivityRecord{f9da72a u0 com.sec.android.app.camera/.Camera t3967} | |
if DEBUG: print 'open_camera'.ljust(20), clock() | |
r = check_output(['adb','shell','dumpsys','activity','activities']) | |
try: | |
app = re.search('mFocusedActivity(.*)',r).group(1) | |
except AttributeError: | |
app = 'Unknown' | |
# if not open camera app:adb shell am start -a android.media.action.IMAGE_CAPTURE | |
if not 'com.sec.android.app.camera' in app: | |
r = check_output(['adb','shell','am','start','-a','android.media.action.IMAGE_CAPTURE']) | |
if DEBUG: print r.strip() | |
sleep(2) | |
def clear_camera_folder(): | |
#delete from phone: adb shell rm /sdcard/DCIM/Camera/* | |
if DEBUG: print 'clear_camera_folder'.ljust(20), clock() | |
r = check_output(['adb','shell','rm','/storage/emulated/0/DCIM/Camera/*']) | |
if DEBUG: print r.strip() | |
def press_camera_button(): | |
#condition 1 screen on 2 camera open: adb shell input keyevent = CAMERA | |
if DEBUG: print 'press_camera_button'.ljust(20), clock() | |
call(['adb','shell','input','keyevent = CAMERA']) | |
sleep(1) | |
def transfer_img(path): | |
#looking for last file in DCIM/Camera: NO NEED cause we just have 1 picture (clear folder before capture) | |
#copy to PC: adb pull /sdcard/DCIM/Camera/ c:/temp | |
if DEBUG: print 'screen transfer_img'.ljust(20), clock() | |
r = check_output(['adb','pull','/storage/emulated/0/DCIM/Camera/', str(path)]) | |
if DEBUG: print r.strip() | |
def screen_off(): | |
# assume it already on: adb shell input keyevent = POWER | |
if DEBUG: print 'screen_off'.ljust(20), clock() | |
r = check_output(['adb','shell','input','keyevent = POWER']) | |
if DEBUG: print r.strip() | |
# just for fun | |
import os | |
import glob | |
def open_file(): | |
fl = glob.glob('c:/temp/*.jpg') | |
last_file = max(fl, key=os.path.getctime) | |
os.startfile(last_file) | |
DEBUG = True | |
capture() | |
open_file() |
Very useful! I had a better result in my phone using the intent STILL_IMAGE_CAMERA
instead of IMAGE_CAPTURE
.
IMAGE_CAPTURE
opens some sort of subset of the camera application and even requires confirmation for the photos taken and the other one opens the actual camera application that I'm used to, with all the settings.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
does not work with python3