J2ME 摄像头拍摄照片操作集合(原创)

来源:互联网 发布:花与剑js为什么禁 编辑:程序博客网 时间:2024/04/28 01:16

原帖地址:http://topic.csdn.net/u/20080707/19/ed7dd69a-3929-4f0b-8b38-de00d7716e11.html

 

先导入

import javax.microedition.media.MediaException; import javax.microedition.media.Player; import javax.microedition.media.control.VideoControl; 然后 //创建1个摄像头控制对象 private VideoControl videoControl = null; try { player = Manager.createPlayer("capture://video"); player.realize(); //取得摄像头控制权 videoControl = (VideoControl)player.getControl("VideoControl"); if (videoControl == null) { discardPlayer(); } else { //初始化 videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);         takeSnapshot(); } catch (IOException ioe) { discardPlayer(); } catch (MediaException me) { discardPlayer(); } catch (SecurityException se) { discardPlayer(); } } private void discardPlayer() { if (player != null) { player.close(); player = null; } videoControl = null; } 最后 //取得拍摄到的图片 private void takeSnapshot() { if (player != null) { try { byte[] pngImage = videoControl.getSnapshot(null); midlet.cameraCanvasCaptured(pngImage); } catch (MediaException me) { message1 = "MediaException;"; message2 = me.getMessage(); } } } 拍完照后 返回摄像模式记得要 try { videoControl.setVisible(false); player.stop(); } catch (MediaException me) { message2 = me.getMessage(); }   重点是 byte[] pngImage = videoControl.getSnapshot(null); 取得的 图片是字节流 就 可以 方便存储在 RMS里面 了 , 稍微简单的 代码如下 import javax.microedition.rms; //打开RMS private RecordStore rs = null; try{ rs=RecordStore.openRecordStore("testPng",true,AUTHMODE_PRIVATE,true); }catch(RecordStoreFullException e){} catch(RecordStoreException ee){}//添加数据 int rsid = 0;//非常重要从 RMS取得图片数据要 用 到 try{ rsid = rs.addRecord(pngImage,0,pngImage.length); }catch(RecordStoreFullException e){} catch(RecordStoreException ee){} //读取 byte[] getPngData; 用 getPngData = getRecord(rsid); LZ 去 看 下 J2ME的 API 里面虽然没例子 但是用法很 明确。···我 已经说 的 很详细了 ,

  还原图片 用 Image.createImage()方法 用 我 上述的 方法应该 能 解决摄像所有问题

原创粉丝点击