0

i need to write a java code to execute some command for automation purpose. what i want to achieve is pushing image to camera.

i tried to write a code to input text via adb and it works.. it looks like this

public void inputTextByCode(String deviceId, String input) throws IOException{  
        welcomeTheTester();
        String commandStr = "input keyboard text ";

        Process process = null;
        String commandString = "";
        if(deviceId != null) {
            commandString = String.format("%s", "adb -s " + deviceId + " shell " + commandStr + input);
        } else {
            commandString = String.format("%s", "adb shell " + commandStr + input);
        }

        System.out.print("Command is "+commandString+"\n");
        try {
            process = ProcessHelper.runTimeExec(commandString);
        } catch (IOException e) {
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.print(line+"\n");
        }
    }

it will produce adb command as

adb -s 192.168.28.101:5555 shell input keyboard text testing

could i do the same,, but with camera? how could i push image / some kind of stream to android camera?

thekucays
  • 568
  • 3
  • 9
  • 32
  • Hi, Are you able to connect to camera with adb commands.? If yes.. you have to issue a command adb -s : push /sdcard/ – nkalra0123 Nov 23 '18 at 10:39
  • @nkalra0123 unfortunately not, so i want to automate an app which is a built in camera feature, to scan qr codes – thekucays Nov 23 '18 at 10:43
  • sorry, unable to understand. Are you working with an Android camera, or with an app on the mobile phone which has a camera feature.? – nkalra0123 Nov 23 '18 at 11:05
  • @nkalra0123 on an app that has camera feature exactly.. is that possible to push the image in that situation? – thekucays Nov 23 '18 at 11:26
  • If you have an app, then why do you want to push the image, just save the image from your java code. check this question https://stackoverflow.com/questions/8588838/android-camera-save-image-into-a-new-folder-in-sd-card – nkalra0123 Nov 23 '18 at 11:29
  • @nkalra0123 no, i want to automate test the app, for the qr code. i'm not the creator of the app, i just want to perform automate test to scan qr code – thekucays Nov 25 '18 at 18:23
  • @thekucays did you manage to solve this? I want to do exactly the same - stream/push an image file to a running camera app, no matter which camera app is running – Lior Iluz Dec 11 '18 at 08:23
  • thekucays and @LiorIluz, I am also needing this for integration test purposes, do you guys found a way to do that? – lpFranz Nov 10 '22 at 14:45

1 Answers1

0

You have to create a dummy camera app. Most app which require image capture from camera implement this logic.

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
   startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}

and implement this code to get the image from camera.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        mImageView.setImageBitmap(imageBitmap);
    }
}

So, you have to create a dummy camera app which should have an Activity like this

<activity
 android:name="com.android.camera.CaptureActivity"
 android:label="@string/app_name"
 android:theme="@style/Theme.Camera"
 android:configChanges="orientation|screenSize|keyboardHidden"
 android:windowSoftInputMode="stateAlwaysHidden|adjustPan">
 <intent-filter>
 <action android:name="android.media.action.IMAGE_CAPTURE" />
 <category android:name="android.intent.category.DEFAULT" />
 </intent-filter>
</activity>

and you should return something like this.

Bitmap bitmap = CameraUtil.makeBitmap(data, 50 * 1024);
bitmap = CameraUtil.rotate(bitmap, orientation);
Log.v(TAG, "inlined bitmap into capture intent result");
mActivity.setResultEx(Activity.RESULT_OK,
Intent("inline-data").putExtra("data", bitmap));
mActivity.finish();

In this way your QR code scanner app will think it has opened a legit camera app, but your dummy app will be opened, and your app will return the image which you want

nkalra0123
  • 2,281
  • 16
  • 17