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 SDK下载

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值