用书面方式的NdefMessage图像 [英] Writting an image using NdefMessage

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

问题描述

我试图写在NDEF标记的图像,目前,我能够写出来,但是当我试图用任何市场应用程序读取它,他们把它像一个文本消息。这里是我的code的一块写图片:

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

我也tryed书面方式形象是这样的:

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" :

文本消息非常writen,可以正常读取。我试图用createMime(字符串MIME类型,字节[]数据),但这种方法似乎未定义.I've试图COM preSS与结果相同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标签读取的名片,什么也没找到后。我决定创建我自己的一种名片和读它自己。这里是code我以前写使用NDEF消息卡:

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

和这里是code的阅读部分:

and here is the code for the reading part :

        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加密有很大帮助COM preSS图像不失去太多的质量。标签上的转移需要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天全站免登陆