gpt4 book ai didi

javascript - 将十六进制字符串转换为 Blob 并使用 AngularJS 显示 PNG 图片

转载 作者:行者123 更新时间:2023-11-29 10:05:59 25 4
gpt4 key购买 nike

我有一个网页(我/我正在使用 angularjs 1.4.8),我正在尝试显示来 self 的 GET url 请求的图像。

这是页面代码(我有一个网格,如果适用,我/我会显示预览):

<div ng-show="message.message == null && message.is_image != null">
<a href="#" ng-click="downloadFile(message.id_message)">
<img data-ng-src="data:image/{{message.image_resolution}};base64,{{message.image_preview}}"/>
</a>
</div>

所以,我得到了带有这个 blob 字段的 cassandra DB,我的 Json 看起来像:

created_date:"2017-03-31 22:05:42.284Z"
id_message:"e6e2a5cb-ec25-472f-a59b-3f16a3a8afa9"
id_user_link:"47ed65bf-5520-4901-88c8-01980ffbcd4d"
id_user_sent:"3495c2de-c93c-4323-8e48-1fcecbfde625"
image_length:174443
image_name:"5.png"
image_preview:"0x89504e470d0a1a0a0000000d49484452000007800000039a080600000079a04f28000038714944415478daecd9496e55570045d13bfff124d442c654016320c4d4219832046308a132087199c26ba4f1fed65ad29ec0e99e71ec97635392244992244992244992b4f90d23489224499
...
... some other 90 lines of symbols
...
00000108401d8006c0096244906600000000008c2006c0036004b922403300000000004610036001b802549920118000000008230001b800dc09224c9000c000000004118800dc00660499264000600000080200cc0066003b024493200030000004010066003b001589224198001000000200803b001d8002c49920cc000000000108401d8006c0096244906600000000008c2006c0036004b92a4ff95fe0ffc7d46dd1b63a2b10000000049454e44ae426082"
image_resolution:"png"
is_image:1
message:null

但是我的网页中没有图片(只有断开的图片链接图标): enter image description here我研究过

Angularjs showing image blob

Display blob image in html with angularjs

AngularJS - Show byte array content as image

但这无济于事。我尝试了以下代码的一些变体:页:

<img data-ng-src="data:image/{{message.image_resolution}};base64,{{b64encoded(message.image_preview)}}"/>

js:

 $scope.b64encoded = function(image_preview){
//btoa(String.fromCharCode.apply(null, response.data[0].ClassImage.data));
$compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|file|ftp|blob):|data:image_preview\//);
return btoa(String.fromCharCode.apply(null, image_preview));
}

已解决最后,这不是关于 AngularJS 或 blob 的问题——这是一个 Java 问题:byte[] previewSizeByte = baos.toByteArray(); 我将这个存储为 blob,所以,现在我得到了一个文本字段,我的 Java 代码看起来像(我决定使用 BufferedImage 进行预览):String base64String = imgToBase64String(preview, fileFormat);

private String imgToBase64String(BufferedImage preview, String fileFormat) {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ImageIO.write(preview, fileFormat, Base64.getEncoder().wrap(os));
return os.toString(StandardCharsets.ISO_8859_1.name());
} catch (final IOException ioe) {
throw new UncheckedIOException(ioe);
}
}

非常感谢 stackoverflow 成员的评论和回答,他们非常有帮助

最佳答案

CassandraDB 似乎正在将图像数据作为十六进制字符串发送。将其作为 base64 string 发送会更有效率而且会更容易使用。

这是一个将十六进制字符串转换为 image/png Blob 的函数并显示图像:

angular.module("myApp",[]).controller("myVm", function($scope) {
var vm = $scope;

var testHex =
["0x89504e470d0a1a0a0000000d494844520000003c00000028040300000050",
"9584cc0000001b504c5445000000ffffff1f1f1f7f7f7f3f3f3f9f9f9f5f",
"5f5fdfdfdfbfbfbf2cb790f6000000097048597300000ec400000ec40195",
"2b0e1b000000b749444154388ded90cf0a83300cc63faaf5394a5defc56c",
"ee2a0c760e2a3b0b6e3ec7c0175f5aff1e77da657ea40dcd2ff90a010efd",
"9772a2f3f6ea4b830e121915b1a04e859999066a4b1801562dec544c3d36",
"cc723506ac9791809538f564af54055c33f8861d76d0cacfd30efc9450c3",
"b0e20189e28847aac5397458b7e2175d4cde4ed37252cff7d83ce367c849",
"b56014ecf638fa28bf62cd49b7c3e9a384f86764269cbde5bf665b969230",
"31adb25feffdd02ff50109f91bbd7897f34a0000000049454e44ae426082"]
.join('');

vm.hex = testHex;
vm.imgUrl = URL.createObjectURL(toPngBlob(testHex));

function toPngBlob(str){
var hexStr = str.slice(2);
var buf = new ArrayBuffer(hexStr.length/2);
var byteBuf = new Uint8Array(buf);
for (let i=0; i<hexStr.length; i+=2) {
byteBuf[i/2] = parseInt(hexStr.slice(i,i+2),16);
}
var blob = new Blob([byteBuf], {type: "image/png"});
return blob;
};

});
<script src="//unpkg.com/angular/angular.js"></script>

<body ng-app="myApp" ng-controller="myVm">
<h1>Convert hex string to PNG Blob</h1>
{{hex}}<br>
{{imgUrl}}<br>
<img ng-src="{{imgUrl}}">

</body>

关于javascript - 将十六进制字符串转换为 Blob 并使用 AngularJS 显示 PNG 图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43174535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com