简体   繁体   English

根据未压缩的原始PNG数据创建PNG

[英]Creating a PNG from uncompressed raw PNG data

I am using this Android Screenshot Library to capture screenshots. 我正在使用这个Android屏幕截图库来捕获屏幕截图。 I want to show this screenshot in web browser. 我想在网络浏览器中显示此屏幕截图。

I have written a small socket app on PC which connects to Android phone and encode the captured screenshot to base64. 我在PC上编写了一个小型套接字应用程序,该应用程序可连接到Android手机,并将捕获的屏幕截图编码为base64。

String base64 = Base64.getEncoder().encodeToString(bytes);

This base64 is being sent to web page over websocket. 该base64通过websocket发送到网页。 Below is my html/JS code which should draw the image on canvas. 下面是我的html / JS代码,应该在画布上绘制图像。 I have stored the base64 image data in a text file for test purpose. 我已将base64图像数据存储在文本文件中以进行测试。

<div style="background: aqua; width: 500px; height: 660px;">

    <canvas style="width: 480px; height: 640px;" id="sim">
    </canvas>
</div>

<script type="text/javascript">
    var ctx;
    var width = 480;
    var height = 640;

    (function start() {
        ctx = document.getElementById("sim").getContext("2d");
        loadFromFile();
    })();

    function loadImage(data) {

        var image = new Image();
        image.onload = function() {
            //this part never executes
            ctx.drawImage(image, 0, 0);
            console.log("images loaded!");
        }

        image.src = "data:image/png;base64," + data;
    }

    function loadFromFile() {
        //please get data from **https://app.box.com/s/e19ww51fd37h4sy8getjnag31ud6tpp9**
        var file = "imagefile.txt";

        var rawFile = new XMLHttpRequest();
        rawFile.open("GET", file, true);

        rawFile.onreadystatechange = function() {
            if (rawFile.readyState === 4) {
                if (rawFile.status === 200 || rawFile.status == 0) {
                    var data = rawFile.responseText;
                    console.log("data loaded: " + data.length);

                    loadImage(data);
                }
            }
        }
        rawFile.send(null);
    }
</script>

For convenience, here is the JsFiddle of same code. 为了方便起见,这是相同代码的JsFiddle

Please point to right algorithm as how to generate the PNG from the raw uncompressed data. 请指向正确的算法,以了解如何从原始未压缩数据生成PNG。 code sample would be great ;) 代码示例会很棒;)

Thanks 谢谢

public static String encodeTobase64(Bitmap image)
{
Bitmap immagex=image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);

Log.e("LOOK", imageEncoded);
return imageEncoded;
}

public static Bitmap decodeBase64(String input) 

{
byte[] decodedByte = Base64.decode(input, 0);
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length); }

Note that 注意

1) set dimension of canvas not by style or css, only this way 1)仅通过这种方式设置画布的尺寸而不是通过样式或css

<canvas width="480" height="640"> 

or 要么

canvas //canvas variable
canvas.width = 480;
canvas.height = 640;

2) To draw image with dimension of canvas, do this way 2)要以画布尺寸绘制图像,请执行以下方法

ctx.drawImage(image, 0, 0, image.width,image.height);

or 要么

canvas.width=image.width; canvas.height=image.height; ctx.drawImage(image, 0, 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM