65.9K
CodeProject 正在变化。 阅读更多。
Home

如何创建诺基亚 OTA 图片消息

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.10/5 (7投票s)

2006 年 10 月 27 日

CPOL
viewsIcon

56335

downloadIcon

869

如何创建移动OTA图片消息。

简介

本示例演示如何为移动设备创建图片消息(*.ota 文件)。OTA 文件是 72 x 28 像素的位图。

本示例还将 Windows 黑白 72 x 28 像素位图文件转换为 OTA 文件。

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
namespace OTAViewer
{
    public class OTAFile
    {
        public const int WIDTH = 72;
        public const int HEIGHT = 28;
        private const int DATASIZE = WIDTH / 8 * HEIGHT;
        byte[,] m_Bitmap = 
        new byte[WIDTH / 8, HEIGHT];
     
        private OTAFile()
        {
        }

        /// <summary>
        /// Represents a (71, 27) Bitmap.
        /// </summary>
        /// <param name="x">X</param>
        /// <param name="y">Y</param>
        /// <returns>True if Pixel is set.</returns>
        public bool this[int x, int y]
        {
            get
            {
                byte b = m_Bitmap[x / 8, y];
                byte bit = (byte)(7 - x % 8);
                byte t = (byte)Math.Pow(2, bit);
                return (b & t) == 0;
            }
            set
            {
                int xx = x / 8;
                byte bit = (byte)(7 - x % 8);
                byte t = (byte)(1 << bit);
                byte i = (byte)(byte.MaxValue - t);
                if (value)
                {
                    m_Bitmap[xx, y] |= t;
                }
                else
                {
                    m_Bitmap[xx, y] &= i;
                }
            }
        }

        public Bitmap ToBitmap()
        {
            Bitmap bmp = new 
            Bitmap(72, HEIGHT);
            for (int i = 0; i < 72; i++)
            {
                for (int j = 0; j < HEIGHT; j++)
                {
                    if (this[i, j])
                    {
                        bmp.SetPixel(i, j, Color.White);
                    }
                    else
                    {
                        bmp.SetPixel(i, j, Color.Black);
                    }
                }
            }
            return bmp;
        }

        public byte[] Data
        {
            get
            {
                byte[] s = new byte[DATASIZE + 4];
                s[0] = 0;
                s[1] = 72;
                s[2] = HEIGHT;
                s[3] = 1;
                int k = 4;
                for (int j = 0; j < HEIGHT; j++)
                {
                    for (int i = 0; i < 9; i++)
                    {
                        s[k++] = m_Bitmap[i, j];
                    }
                }
                return s;
            }
        }

        public static OTAFile FromBitmap(Image img)
        {
            if (img.Width != 72 || 
              img.Height != HEIGHT || img.PixelFormat != 
              System.Drawing.Imaging.PixelFormat.Format1bppIndexed)
            {
                throw new Exception(string.Format("A " + 
                  "72x{0} Pixel Black-and-White image Required.", 
                  HEIGHT));
            }
            Bitmap bmp = new Bitmap(img);
            OTAFile ota = new OTAFile();
            for (int i = 0; i < 72; i++)
            {
                for (int j = 0; j < HEIGHT; j++)
                {
                    ota[i, j] = (bmp.GetPixel(i, j) != 
                      Color.FromArgb(255, 255, 255));
                }
            }
            return ota;
        }

        public static OTAFile FromFile(string file)
        {
            OTAFile ota = new OTAFile();
            byte[] buf = new byte[DATASIZE];
            Stream fs = File.OpenRead(file);
            fs.Position = 4;
            fs.Read(buf, 0, DATASIZE);
            fs.Close();
            int x;
            int y;
            for (int i = 0; i < DATASIZE; i++)
            {
                x = i % 9;
                y = i / 9;
                ota.m_Bitmap[x, y] = buf[i];
            }
            return ota;
        }
    }
}

玩得开心!

如何在 Nokia OTA 图片消息中创建 - CodeProject - 代码之家
© . All rights reserved.