有没有办法从base64编码的图像数据中获取图像宽度和高度同步?可能是标题信息。
我必须非常清楚。我对异步方法不感兴趣。如果您想知道async不起作用的原因,请继续。甚至不评论。
这是我编码数据的示例代码。编码是同步的。我正在使用此call:
var base64ImageData:String = DisplayObjectUtils.getBase64ImageDataString(displayObject, DisplayObjectUtils.PNG, null, true);
这是我解码数据的代码。我在AS3中发现的唯一解码方法是异步的。我正在使用此call:
var bitmapData:BitmapData = DisplayObjectUtils.getBitmapDataFromBase64(bitmapDataString);
让我再说一遍,我正在寻找一个同步动作。它不能是异步的。
更多信息:
我正在使用ActionScript3,它是JavaScript的兄弟。如果它在JavaScript中工作,它很可能在AS3中工作。但我无法使用dom。不向dom添加图像和检查值。
有用的黑客:
如果可以在base64流中添加宽度和高度,然后稍后再获取它也可以。
答案 0 :(得分:3)
这仅适用于png 首先要做的是转换base64做一个缓冲区(Uint8array)然后读取字节16-20(宽度)和20-24(高度)作为int32值
function getPngDimensions(base64) {
let header = base64.slice(0, 50)
let uint8 = Uint8Array.from(atob(header), c => c.charCodeAt(0))
let dataView = new DataView(uint8.buffer, 0, 28)
return {
width: dataView.getInt32(16),
height: dataView.getInt32(20)
}
}
// Just to get some random base64 images
var random = (bottom, top) => Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom
var canvas = document.createElement('canvas')
canvas.width = random(10, 100)
canvas.height = random(10, 100)
console.log(`canvas width: ${canvas.width}, height: ${canvas.height}`)
var base64 = canvas.toDataURL().split(',')[1]
console.log(base64)
var dimensions = getPngDimensions(base64)
console.log(dimensions)

答案 1 :(得分:2)
以下不起作用,但它试图将@Endless答案转换为AS3。到目前为止我有这个:
protected function windowedapplication1_applicationCompleteHandler(event:FlexEvent):void {
var base64ImageData:String = DisplayObjectUtils.getBase64ImageDataString(this, DisplayObjectUtils.PNG, null, true);
var size:Object = getPngDimensions(base64ImageData);
}
public function getPngDimensions(base64:String):Point {
base64 = base64.split(',')[1];
trace(base64.length);
base64 = base64.replace(/\n/g, "");
trace(base64.length);
//var header:String = base64.slice(0, 52); // 50 gave error later on
var header:String = base64.slice(0, base64.length);
var base64Decoder:Base64Decoder = new Base64Decoder();
var byteArray:ByteArray;
base64Decoder.reset();
base64Decoder.decode(header);
// base64Decoder.toByteArray(); ==>
// Error: A partial block (2 of 4 bytes) was dropped. Decoded data is probably truncated!
// added the whole length (52 works as well)
byteArray = base64Decoder.toByteArray();
byteArray.position = 0;
//var width:String = byteArray.readUTFBytes(0);
//var value:ByteArray = new ByteArray();
//value.readBytes(byteArray, 0, 28);
var thing:Number = byteArray.readDouble(); // -8.091055181950927E-264
byteArray.position = 0;
var thing2:Number = byteArray.readFloat(); // -2.5073895107184266E-33
byteArray.position = 0;
var width:Number = byteArray.readFloat();
byteArray.position = 16;
var width:Number = byteArray.readFloat();
byteArray.position = 20;
var width:Number = byteArray.readFloat();
byteArray.position = 28;
var width:Number = byteArray.readFloat(); // 1.1074230549312416E-38
byteArray.position = 0;
var other:String = byteArray.readUTFBytes(byteArray.length);
// other == "PNG\r\n\n"
var point:Point = new Point();
point.x = int(width);
point.y = int(height);
return point;
}
如果我使用ByteArray.readUTFBytes()
我会得到" PNG"结果但没有宽度或高度。
答案 2 :(得分:1)
“如果我使用
ByteArray.readUTFBytes()
我会得到”PNG“,但没有宽度或高度。”
不要将字节读取为UTF
格式,因为它只提取文本字符。你想要小数值。
readDouble
和readFloat
太多,每次操作读取8个字节。 PNG格式每个宽度或高度值仅使用4个字节,因此只读取(无符号)整数。 read
操作都会自动移动byteArray位置。例如,要知道bytePos-1的值,你必须“走”/读过它,所以你最终得到bytePos-2,或者对于Double类型,你最终会得到bytePos-9。 查看代码的重新修复是否获得了宽度和宽度的预期值。身高......
public function getPngDimensions(base64:String):Point
{
base64 = base64.split(',')[1];
trace("Base64 length A : " + base64.length);
base64 = base64.replace(/\n/g, "");
trace("Base64 length B : " + base64.length);
//var header:String = base64.slice(0, 52); // 50 gave error later on
var header:String = base64.slice(0, base64.length);
var base64Decoder:Base64Decoder = new Base64Decoder();
var byteArray:ByteArray;
base64Decoder.reset();
base64Decoder.decode(header);
// base64Decoder.toByteArray(); ==>
// Error: A partial block (2 of 4 bytes) was dropped. Decoded data is probably truncated!
// added the whole length (52 works as well)
byteArray = base64Decoder.toByteArray();
byteArray.position = 0;
//# Get Width and Height values
//# type Int is 4 bytes long so pointer auto-moves ahead by 4 bytes during Read action
byteArray.position = 16; //start position for values...
var width:int = byteArray.readUnsignedInt(); //reads 4 bytes, stops at Pos 20
var height:int = byteArray.readUnsignedInt(); //reads 4 bytes, stops at Pos 24
//# Double check (if needed)
//trace("PNG width : " + width);
//trace("PNG height : " + height);
//# Return as Point
var point:Point = new Point();
point.x = width; point.y = height;
return point;
}