Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Convert Screenshot Stream Properly? #62

Open
anjiuzhe opened this issue Jun 7, 2024 · 0 comments
Open

How to Convert Screenshot Stream Properly? #62

anjiuzhe opened this issue Jun 7, 2024 · 0 comments

Comments

@anjiuzhe
Copy link

anjiuzhe commented Jun 7, 2024

hello,In Java I take a screenshot of the device with the following adb command, and when I convert the stream to an image, I get an incorrect image, the code looks like this:

String command = String.format("adb shell CLASSPATH=/data/local/tmp/minicap-devel/minicap-debug.apk app_process /system/bin io.devicefarmer.minicap.Main -P %s@%s/0 -s", screenSize, screenSize);
ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
Process process = processBuilder.start();
InputStream inputStream = process.getInputStream();
BufferedInputStream bis = new BufferedInputStream(inputStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = bis.read(buffer)) != -1) {
    byteArrayOutputStream.write(buffer, 0, bytesRead);
}
byte[] imageBytes = byteArrayOutputStream.toByteArray();
InputStream byteArrayInputStream = new ByteArrayInputStream(imageBytes);
BufferedImage bufferedImage = ImageIO.read(byteArrayInputStream);
if (bufferedImage == null) {
    throw new IOException("Failed to decode image from input stream.");
}
File outFile = new File(savePath);
ImageIO.write(bufferedImage, "jpg", outFile);

the image looks like this:

However, when I modify the source code of the minicap-debug.apk, the Screenshot Output sent to the screenshot stream is changed here to look like this:

class ScreenshotOutput(private val output: PrintStream) : DisplayOutput() {

    override fun send() {
        val writer = PrintWriter(output)
        writer.use {
            val encodedImage: String = Base64.encodeToString(imageBuffer.toByteArray(), Base64.DEFAULT)
            it.write(encodedImage)
            it.flush()
        }
    }
}

On the java side, the screenshot stream is decoded in Base 64 and then converted to obtain the correct image,java changed here to look like this:

String command = String.format("adb shell CLASSPATH=/data/local/tmp/minicap-devel/minicap-debug.apk app_process /system/bin io.devicefarmer.minicap.Main -P %s@%s/0 -s", screenSize, screenSize);
ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
Process process = processBuilder.start();
InputStream inputStream = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
    stringBuilder.append(line);
}
reader.close();
// Decode the Base64 string to byte array
byte[] imageBytes = Base64.getDecoder().decode(stringBuilder.toString());
// Convert byte array to BufferedImage
InputStream byteArrayInputStream = new ByteArrayInputStream(imageBytes);
BufferedImage bufferedImage = ImageIO.read(byteArrayInputStream);
if (bufferedImage == null) {
    throw new IOException("Failed to decode image from input stream.");
}
// Write the BufferedImage to file
File outFile = new File(savePath);
ImageIO.write(bufferedImage, "jpg", outFile);

The image I get after the modification is very correct, I want to know how to parse the image correctly when not using Base 64, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant