使用NdefMessage写入图像 [英] Writing an image using NdefMessage

查看:110
本文介绍了使用NdefMessage写入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to write an image on a Ndef Tag, currently, i am able to write it, but when I try to read it with any market application, they treat it like a text message. here is my piece of code writing the image :

        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});

我也尝试过这样写图像:

i also tryed writting the image this way :

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

使用createMimeRecord方法:

with the method 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"之类的应用程序读取图像时得到的结果:

Here is the result i get when trying to read my image with applications like "TagInfo" :

文本消息写得很好,可以正常阅读.我尝试使用"createMime(String mime类型,byte []数据)",但此方法似乎未定义".我尝试以Jpeg格式压缩位图图像,结果相同.找到了通过NdefMessages发送图像的示例,但没有找到任何建议吗?

The text message is well writen and can be read normally. I've tried to use the "createMime(String mime type, byte[] data) but this method seems "undefined".I've tried to compress the bitmap image in Jpeg format with the same result. Also I've tried to find examples of sending images via NdefMessages, but didn't find any. Any suggestion ?

推荐答案

最后,在搜索应用程序后,写并读取了带有NFC标签的名片,却一无所获.我决定创建自己的名片并自己阅读.这是我用来使用Ndef Message写卡的代码:

Finally after searching for an application writting and reading Business card on NFC tag and finding nothing. I decided to create my own kind of business card and read it myself. here is the code i used to write the card using 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秒,但解决方案效果很好.

The Jpeg encryption helps a lot to compress the image without loosing too much quality. The transfer on the tag take 2-3 seconds but the solution works well.

这篇关于使用NdefMessage写入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆