gpt4 book ai didi

android - 使用 NdefMessage 写入图像

转载 作者:行者123 更新时间:2023-11-29 01:54:34 25 4
gpt4 key购买 nike

我正在尝试在 Ndef 标签上写一个图像,目前,我可以写它,但是当我尝试用任何市场应用程序读取它时,它们将其视为文本消息。这是我编写图像的代码:

        Bitmap mBitmap = Bitmap.createScaledBitmap(mPhoto, 100, 100, true);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
NdefRecord picRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, "image/png".getBytes(), null, byteArray);
String informations = "name: "+name + "\ntitle: " + title + "\naddress: "+ address + "\ncity: "+ city + "\nphone: "+ phone + "\nmail: "+mail;
NdefRecord textRecord = createTextRecord(informations);
NdefMessage message = new NdefMessage(new NdefRecord[]{picRecord, textRecord});

我也试过这样写图片:

        NdefMessage msg = new NdefMessage(new NdefRecord[] {createMimeRecord("image/png", byteArray), textRecord});

使用方法 createMimeRecord :

        public NdefRecord createMimeRecord(String mimeType, byte[]payload) {
byte[] mimeBytes = mimeType.getBytes(Charset.forName("USASCII"));
NdefRecord mimeRecord = new
NdefRecord(NdefRecord.TNF_MIME_MEDIA,
mimeBytes, new byte[0], payload);
return mimeRecord;
}

这是我尝试使用“TagInfo”等应用程序读取图像时得到的结果:enter image description here

短信写得很好,可以正常阅读。我试过使用“createMime(String mime type, byte[] data) 但这种方法似乎是“未定义的”。我试过以相同的结果压缩 Jpeg 格式的位图图像。我也试过找到通过 NdefMessages 发送图像的示例,但没有找到。有什么建议吗?

最佳答案

最后,在搜索一个在 NFC 标签上写入和读取名片的应用程序后,最终一无所获。我决定创建自己的名片并自己阅读。这是我用来使用 Ndef Message 写卡片的代码:

        Bitmap mBitmap = mPhoto;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
byte[] byteArray = stream.toByteArray();
NdefRecord picRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, "image/jpeg".getBytes(), null, byteArray);
String informations = "name: "+name + "\ntitle: " + title + "\naddress: "+ address + "\ncity: "+ city + "\nphone: "+ phone + "\nmail: "+mail + "\n";
NdefRecord textRecord = createTextRecord(informations);
NdefMessage message = new NdefMessage(new NdefRecord[]{picRecord, textRecord});

这是阅读部分的代码:

        NdefRecord picRecord = records[0];
NdefRecord infoRecord = records[1];
byte[] picload = picRecord.getPayload();
byte[] infoload = infoRecord.getPayload();
Bitmap photo = BitmapFactory.decodeByteArray(picload, 0, picload.length);
String textEncoding = ((infoload[0] & 0200) == 0) ? "UTF-8" : "UTF-16";
int languageCodeLength = infoload[0] & 0077;
String text = null;
try{
String languageCode = new String(infoload, 1, languageCodeLength, "US-ASCII");
text = new String(infoload, languageCodeLength + 1,infoload.length - languageCodeLength - 1, textEncoding);
}catch(Exception e){
Alert("String decoding", e.toString());
return;
}

Jpeg 加密有助于在不损失太多质量的情况下压缩图像。标签上的传输需要 2-3 秒,但该解决方案效果很好。

关于android - 使用 NdefMessage 写入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15945545/

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