Skip to content

Instantly share code, notes, and snippets.

@me7
Last active June 24, 2024 22:35
Show Gist options
  • Save me7/12ad7c823a1b5d1a061dceb4fcd72b87 to your computer and use it in GitHub Desktop.
Save me7/12ad7c823a1b5d1a061dceb4fcd72b87 to your computer and use it in GitHub Desktop.
use adb to capture image and transfer to PC
# 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()
@meutzitzu
Copy link

does not work with python3

@jbonadiman
Copy link

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