BlackBerry 照相编程

Bla ckBe rr y S D K 5 .0 以及之 前的版本 通过 MM A P I 实 现拍照功 能,主 要的功能 包包括:

 

·     j avax. mi cr o e d i t i o n .me d i a

·     j avax. mi cr o e d i t i o n .me d i a.co n t ro l

·     j avax. mi cr o e d i t i o n .amm s. c o nt ro l .c am er a

 

拍照应用 包括如 下几部分 :

 

·     获取黑莓 手机相 机支持信 息,照片 大小, 格式,照 片质量

·     显示并确 定照片 设置

·     打开相机 设备, 设置自动 对焦,显 示摄像 头图像

·     拍照

 

获取支 持的照片规格

 

照片规格 通过 4 种属性来 定义:照 片宽度 ,照片高 度,照片 格式, 和照片质 量。为了 方便 定义照片 规格, 需要一个 照片规格 类 ,比 如叫 Enc o d ing P ro p ertie s ,代 码如 下:

 

 

/**

*   定义照片 规格, 通过函 数 getFullEncoding() 为 拍照函数 VideoContro l. getSnapshot( ) 提供

*   照片定义

*/

public   final class EncodingProperties

{

/**   照片格式     */

private   String   _format ;

 

/** 照片宽   */

private   String   _width ;

 

/** 照片高   */

private   String   _height ;

 

/** 照片质量   */

private   String   _quality ;

 

public void setFormat(String format)

{

_format = format;

}

 

public   void setWidth(String   width)

{

_width = width;

}

   

public   void setHeight(String   height)

{

_height = height;

}

 

public   void setQuality(String   quality)

{

_quality = quality;

}

 

/**

*   VideoControl.getSnapshot( EncodingProperties .getFullEncoding())

*/

public   String   getFullEncoding()

{

StringBuffer   fullEncoding = new   StringBuffer();

 

fullEncoding.append( "encoding=" );

fullEncoding.append( _format );

 

fullEncoding.append( "&width=" );

fullEncoding.append( _width );

 

fullEncoding.append( "&height=" );

fullEncoding.append( _height );

 

fullEncoding.append( "&quality=" );

fullEncoding.append( _quality );

 

return   fullEncoding.toString();

}

}

 

 

 

照片规格 类定义 完毕,下 面要获取 照片规 格,代码 如下:

 

private   EncodingProperties[]   getEncodingList(){

try

{

//   最终需要 的照片规 格

EncodingProperties[] encodings = null;

 

// 获取照片 规格

String encodingString = System. getProperty ( "video.snapshot.encodings" );

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4
 
// 初步解析 照片规 格

String[] properties = StringUtilities. stringToKeywords (encodingString); Vector encodingList = new Vector();

String encoding = "encoding" ; String width = "width" ;

String   height = "height" ;

String   quality = "quality" ;

 


 

 

EncodingProperties   temp = null ;

 

// 开始解析照 片规格并 组装成 我们定 义的 照片规格

for ( int   i   = 0; i < properties. length   ;   ++i)

{

if (   properties[i].equals(encoding))

{

if (temp   != null   &&   temp.isComplete())

{

// Add a new encoding to the list if it has been

//   properly set.

encodingList.addElement( temp );

}

temp   = new   EncodingProperties();

 

//   Set the new encoding's format

++i;

temp.setFormat(properties[i]);

}

else   if (   properties[i].equals(width))

{

// Set the new encoding's width

++i;

temp.setWidth(properties[i]);

}

else   if (   properties[i].equals(height))

{

//   Set the new encoding's height

++i;

temp.se tHeight(properties[i]);

}

else   if (   properties[i].equals(quality))

{

// Set the new encoding's quality

++i;

temp.setQuality(properties[i]);

}

}

 

//   If there is a leftover complete encoding, add it.

if (temp   != null   &&   temp.isComplete())

{

encodingList.addElement(   temp );

}

 

//   Convert the Vector to an array for later use

encodings   = new   EncodingProperties[   encodingList.size() ];

encodingList.copyInto((Object[])encodings);

}

catch   (Exception   e)

{

// Something is wrong, indicate that there are no encoding options encodings = null ;

}

//   返回需要 的照片 规格

return   encodings;

}

 


 

 

 

显示并 确定照片设置

 

我们已经 有了 Enc o d ing P r o p erti es ,可 以重写 to S tri ng () 函数获取 照片规 格并 用 Rad io Bu tt o n

的格式显 示出来 ,如下图 所示:

 

 

  

这是 Bla ck Berr y 9 7 0 0 所支 持的照片 规格。

 

 

 

启动摄 像头准备拍照

 

首先要初 始化摄 像头,然 后启动摄 像头, 设置自动 对焦,再 把展现 摄像的 Fi eld 添加到屏 幕上。

 

 

/**

*   初始化 Player,   VideoControl   和     Vi de oF ie ld

*/

private   void initializeCamera()

{

 


try

{

 

 

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

6
 
// 注意这 里的参数 是   “capture:// video” , Player 作为 摄像 用

Player player = Manager. createPlayer ( "capture://video" );

 

//   预备摄 像

player.realize();

 


 

 

//   获取     VideoControl

_videoControl = (VideoControl)player.getControl( "VideoControl" );

 

// 获取   FocusControl

_focusControl = (FocusControl)

player.getControl( "javax.microedition.amms.control.camera.FocusControl" );

 

if ( _videoControl != null )

{

//   获取显示 Vi de o 的 UI 组件: Field

_videoField = (Field) _videoControl .initDisplayMode

(VideoControl. USE_GUI_PRIMITIVE ,

"net.rim.device.api.ui.Field" );

_videoControl .setDisplayFullScreen( true );

_videoControl .setVisible( true );

}

 

if   ( _focusControl   !=   null )

{

_focusControl .setFocus(FocusControl. AUTO );

}

 

//   启动摄像 头

player.start();

 

// 添加到屏 幕

add(_videoField);

 

}

catch (Exception   e)

{

System.out.println( "ERROR " + e.getClass() + ":        " + e.getMessage());

}

 

}

 

 

 

两个关键 的类分 别是 Vid e o Co n tro l 和 FocusC o n tr o l 。 Vid eo C o n tr o l 控制 在哪 个 UI 组件上 显

示摄像头 捕捉到 的   Vid e o 。这里指 定是在   Fi eld 上 显示   Vid e o ,显 示模式为 全屏。

FocusC o n tro l 控制对 焦, 可以设置 自动对 焦或者自 定对焦。

 

 

 

 

 


 

 

 

拍照

 

拍照要设 定照片 规格,在 使用 Vid e o Co n t ro l 拍 照, 代码如下 :

 

 

/**

*   按指定的 照片规 格拍照

*/

public   Bitmap   takePicture(){

try {

Bitmap   image = null ;

String   encoding = null ;

 

if ( _encodings != null )

{

//   指定照 片规格

encoding = _encodings [ _indexOfEncoding ].getFullEncoding();

}

 

//   指定照片 规格 ,使用 VideoControl 拍照

//   照片规格 范例 :     “ encoding=jpeg&wi dth=1024&height=768&quality=normal ”

byte []   raw = _videoControl .getSnapshot(   encoding );

 

//   组装成图 片

image = Bitmap. createBitmapFromBytes ( raw, 0, -1, 1 );

}

catch (Exception   e)

{

PingAn.errorDialog ( "ERROR " + e.getClass() + ":        " + e.getMessage());

}

 

return   image;

}

 

 

 

这段代码 指定照 片规格并 使用 Vid e o Co n t ro l 拍 照。 照片规格 范例:

 

Strin g e n co d in g = “en co d in g =jpeg&width =1 0 2 4 &heig h t= 7 6 8 &qu ali ty =n o r m al” ;

 

使用照片 规格拍 照:

 

b y te [] ra w = _v id eo C o n tr o l. g et Sn ap shot ( enco d ing ) ;

 

获取的图 片二进 制数据流 可以被组 装成图 片。

  

你可能感兴趣的:(blackberry,编程,encoding,照片,properties,string)