`
lukew
  • 浏览: 51634 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Serializing an Image

 
阅读更多

Serializing an Image

Creating an image from an array of data is an easy task, but to create a byte-array of data from an image is a little more complicated. But it's required if you want to send a modified image to a server.

To create a byte-array of data from an image, we can use the getRGB(..) method in the image class in MIDP 2.0. From the getRGB method we get an int-array of data containing all the ARGB values of each pixel in the image. Integers in java are four bytes, so we need to split each int value into four byte values.

To mask out each of the bytes in the int we can use the 'AND &' operator and the 'shift-right >>' operator. Here's an example:

int ARGB = 0xFFFFFFFF;  // AARRGGBB 
int a = (ARGB & 0xFF000000);
int r = (ARGB & 0x00FF0000);
int g = (ARGB & 0x0000FF00);
int b = (ARGB & 0x000000FF);
// Here we move each bit to the right.
a = (a >> 24); // a = 0x000000FF
r = (r >> 16); // r = 0x000000FF
g = (g >> 8); // g = 0x000000FF
b = b;        // b = 0x000000FF

When we convert the integer to a byte, there are some problems since there are only signed bytes in Java. A byte may contain values between –128 and 127 and from our integer we'll have a value between 0 and 255.

Here are some conversion examples:

Integer val 127 = byte val 127. 
Integer val 128 = byte val –128.
Integer val 255 = byte val –1.
byte ba = (byte)(a);  // if a=0x000000FF (255), then ba = -1
byte br = (byte)(r);
byte bg = (byte)(g);
byte bb = (byte)(b);

We have to loop though each pixel in the image and get each pixel value to our byte-array. When that's done, we can send the image to a server where we can convert the byte-array back to a integer-array and then show our picture.

So, when we convert the byte back to an integer we have to check if the byte value is less than zero.

Int a, r, g, b;
If(ba<0)
     a = 256 – a;

Now our new integer value will be between 0 and 255 and we just have to use the 'shift-left <<' operator and add the values together to get our new int-array.

a = (a << 24);
r = (r << 16);
g = (g << 8);
b = (b);
  0xFF000000 (a)
+ 0x00FF0000 (r)
+ 0x0000FF00 (g)
+ 0x000000FF (b)
= 0xFFFFFFFF (argb)
int ARGB = a+r+g+b;

After the integer-array is recreated we can use the createRGBImage(..) method in the Image class to create our image.

Be aware that the byte-array is quite large as it contains all of the ARGB values for each pixel. If an image is 100*60 px, where each pixel is four bytes, the byte array will be 24kb.

ImageBytearryConvert.rar

分享到:
评论

相关推荐

    Serializing Interfaces in SAP AIF.zip

    sap press doc 解压密码:abap_developer

    Java 问题和分析 Serializing a primitive with ObjectOutStream

    Java 对象序列化和 ObjectOutStream Java 序列化是一种将对象图表示为字节序列的机制,直接支持原始数据类型作为对象的一部分。原始类型的包装类也实现了 Serializable 接口,因此也可以进行序列化。...

    Python Cookbook, 2nd Edition

    Testing Whether an Object Is String-like Recipe 1.4. Aligning Strings Recipe 1.5. Trimming Space from the Ends of a String Recipe 1.6. Combining Strings Recipe 1.7. Reversing a String by ...

    Visual C++ 编程资源大全(英文源码 DLL)

    cryptit.zip Keep sensitive data safe via encryption (130KB)&lt;END&gt;&lt;br&gt;45,gener1.zip Template functions for serializing arbitrary linked nodes.(25KB)&lt;END&gt;&lt;br&gt;46,gener2.zip Template functions for...

    Professional.MFC.with.VC6

    Serializing a Rich Edit Control Uncommon Controls Checked List Boxes CDragListBox Summary Chapter 9: Writing Programs for the Windows Shell Working With the Shell Shell APIs The Shell ...

    Essential Windows Communication Foundation For .NET Framework 3.5

    * Serializing data from .NET types to XML Infosets and representing Infosets “on the wire” * Hosting WCF services via IIS, managed .NET applications, and Windows Activation Services * WCF ...

    C#学习的101个经典例子

    Advanced - Serialization - How-To Serializing Objects Advanced .NET Framework (GDI+) - Animation with GDI+ Advanced .NET Framework (GDI+) - Create a Screensaver with GDI+ Advanced .NET Framework ...

    Java XML and JSON: Document Processing for Java SE, 2nd Edition

    Java XML and JSON: ...Master the JSON format for serializing and transmitting data Code against third-party APIs such as Jackson, mJson, Gson, JsonPath Master Oracle’s JSON-P API in a Java SE context

    toml++tomlplusplus-master TOML配置文件C++库

    Supports serializing to JSON and YAML Proper UTF-8 handling (incl. BOM) C++17 (plus some C++20 features where available, e.g. experimental support for char8_t strings) Doesn't require RTTI Works with ...

    MongoDB的Scala开发包Subset.zip

    Subset is a library to ease extracting fields from MongoDB documents, serializing them back and constructing queries. 标签:Subset

    protocol buffer 3

    protocol buffer 3 详细描述 Protocol buffers are a language-neutral, platform-neutral extensible mechanism for serializing structured data.

    protocol buffers

    a language-neutral, platform-neutral, extensible way of serializing structured data for use in communications protocols, data storage

    Practical Hadoop Ecosystem

    Part IV: Serializing Chapter 7: Apache Avro Chapter 8: Apache Parquet Part V: Messaging & Indexing Chapter 9: Apache Kafka Chapter 10: Apache Solr Chapter 11: Apache Mahout

    mybatis-3.4.6.zip

    Serializing and deserializing cached objects causes NullPointerException. #1084 Invalid error message 'Two methods with same method signature but not providing classes assignable?' in System.err. #...

    ZendFramework中文文档

    9.5.3. Create dates from an array 9.6. Constants for General Date Functions 9.6.1. Using Constants 9.6.2. List of All Constants 9.6.3. Self-Defined OUTPUT Formats with ISO 9.6.4. Self-defined ...

    protobuf-all-3.13.0.tar.gz

    Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler

    FlexJson1.7 Jar包

    Flexjson is a lightweight library for serializing Java objects into JSON. FlexJson,一个轻量级的Jar库,可以将Java对象序列化成Json格式

    可以在QT4中使用的JSON库

    QT4 JSON The **qt-json** project is a simple collection of functions for parsing and serializing [JSON][js] data to and from [QVariant][var] hierarchies.

    Atom-atom,用于序列化Atom Web内容联合格式的库https://crates.io/crates/atom联合.zip

    Atom-atom.zip,Library for serializing the Atom web content syndication format https://crates.io/crates/atom_syndication原子,atom是一个用web技术构建的开源文本编辑器。

    Unity塔防示例

    Unity的UI系统基于Canvas和各种UI元素,如Text、Button和Image,可以方便地创建动态交互界面。 7. **动画和特效**:Unity的Animation系统可以创建和管理角色和物体的动作,而粒子系统可以添加射击效果、爆炸和视觉...

Global site tag (gtag.js) - Google Analytics