问题描述
mVideoControl.getSnapshot("图片格式");在手机里面,这里的图片格式应该怎么填啊,是天"encoding=jpeg"吗?我改成不同的格式就会得到不同的效果,有时候改的格式打开图片时显示格式错误!大家讨论下在J2ME里面这里的图片格式该怎么填!具体问题如下!!大家帮忙看看,下面这程序是首先进入拍照周期设置界面,设置好后按OK就开始拍照,隔一段周期拍照一次,然后保存到手机存储卡内,帮忙看下下面的代码:定时拍照后能保存到手机存储卡里面,可是保存到手机的图片打开时显示“格式错误”,我怀疑是我捕获的时候格式错了,帮忙看看importjava.io.IOException;importjava.io.OutputStream;importjava.util.Calendar;importjava.util.Timer;importjava.util.TimerTask;importjavax.microedition.io.Connector;importjavax.microedition.io.file.FileConnection;importjavax.microedition.lcdui.Alert;importjavax.microedition.lcdui.Canvas;importjavax.microedition.lcdui.Command;importjavax.microedition.lcdui.CommandListener;importjavax.microedition.lcdui.Display;importjavax.microedition.lcdui.Displayable;importjavax.microedition.lcdui.Font;importjavax.microedition.lcdui.Form;importjavax.microedition.lcdui.Graphics;importjavax.microedition.lcdui.TextField;importjavax.microedition.media.Manager;importjavax.microedition.media.MediaException;importjavax.microedition.media.Player;importjavax.microedition.media.control.VideoControl;importjavax.microedition.midlet.MIDlet;//MIDletpublicclassSnapperMIDletextendsMIDletimplementsCommandListener{privateDisplaymDisplay;privateFormmMainForm;//创建播放器对象privatePlayermPlayer;//创建视频控制器接口privateClockCanvascan;privateVideoControlmVideoControl;privateCommandexit=newCommand("退出",Command.EXIT,1);privateCommandok=newCommand("确定",Command.SCREEN,1);privateCommanddelete=newCommand("删除",Command.BACK,1);privateTextFieldtxf=newTextField("拍照频度(S)","",12,TextField.NUMERIC);intb;staticintsum=0;privatestaticintj=0;char[]a;publicSnapperMIDlet(){super();mDisplay=Display.getDisplay(this);can=newClockCanvas();//can.setFullScreenMode(true);//全屏mMainForm=newForm("在线取照");mMainForm.append(txf);mMainForm.addCommand(exit);mMainForm.addCommand(ok);mMainForm.addCommand(delete);mMainForm.setCommandListener(this);}publicvoidstartApp(){//设置ClockCanvas对象为当前显示对象mDisplay.setCurrent(mMainForm);}publicvoidpauseApp(){}publicvoiddestroyApp(booleanunconditional){}publicvoidcommandAction(Commandc,Displayabled){if(c==exit){destroyApp(false);notifyDestroyed();}elseif(c==ok&&(txf.getString().length()>0)){//得到TextField框中的字符串,然后转换成字符数组,再转换成数字a=txf.getString().toCharArray();//得到字符串并转换成数组b=txf.getString().length();//得到字符串长度for(inti=0;i<b;i++)//将字符数组转换成数字{sum*=10;System.out.println(a);sum=sum+a-48;}mDisplay.setCurrent(can);//启动摄像头showCamera();}elseif(c==delete&&(txf.getString().length()>0)){txf.delete(txf.getCaretPosition()-1,1);}}//定时器任务执行classClockTimerTaskextendsTimerTask{privateinti=0;privateintj=sum;publicvoidrun(){i++;if(sum!=0){j=sum;if(i%j==0){newHttpThread().start();//sum为输入的拍照周期,一个周期到后就拍照}}}}//显示照相屏幕privatevoidshowCamera(){try{//创建播放器对象mPlayer=Manager.createPlayer("capture://video");//使摄像头处于就绪状态mPlayer.realize();//创建视频控制器接口mVideoControl=(VideoControl)mPlayer.getControl("VideoControl");//新建Canvas画布对象CameraCanvascanvas=newCameraCanvas(this,mVideoControl);mDisplay.setCurrent(canvas);//启动摄像头mPlayer.start();}catch(IOExceptionioe){handleException(ioe);}catch(MediaExceptionme){handleException(me);}}classHttpThreadextendsThread{byte[]data=null;byte[]img=null;FileConnectionfc=null;OutputStreamos=null;Stringpath=null;publicvoidrun(){j++;try{data=mVideoControl.getSnapshot("encoding=jpeg"+"width=160"+"height=120");//拍照,设置照片格式为png,宽度和大小固定}catch(MediaExceptionex){ex.printStackTrace();}path="file://localhost/"+"PhoneDisk:/"+"zmy"+j+"."+"jpeg";try{fc=(FileConnection)Connector.open(path);if(!fc.exists())fc.create();else{fc.truncate(0);}os=fc.openOutputStream();os.write(data,0,data.length);os.close();os.flush();fc.close();os=null;fc=null;}catch(IOExceptionioe){System.out.println("异常=");}}}//处理异常privatevoidhandleException(Exceptione){Alerta=newAlert("Exception",e.toString(),null,null);a.setTimeout(Alert.FOREVER);mDisplay.setCurrent(a,mMainForm);}//低级用户界面,画相框classCameraCanvasextendsCanvasimplementsCommandListener{privateCommandcmdExit=newCommand("退出",Command.EXIT,1);publicCameraCanvas(SnapperMIDletmidlet,VideoControlvideoControl){intwidth=getWidth();intheight=getHeight();this.addCommand(cmdExit);this.setCommandListener(this);videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO,this);try{videoControl.setDisplayLocation(2,2);//设置取照界面的左上角坐标videoControl.setDisplaySize(width-4,height-4);//设置取照界面大小}catch(MediaExceptionme){try{videoControl.setDisplayFullScreen(true);}catch(MediaExceptionme2){}}videoControl.setVisible(true);}publicvoidcommandAction(Commandc,Displayabled){if(c==cmdExit){destroyApp(false);notifyDestroyed();}}publicvoidpaint(Graphicsg){intwidth=getWidth();intheight=getHeight();//绘制VideoControl控件的边框g.setColor(0x000000);g.drawRect(0,0,width-1,height-1);g.drawRect(1,1,width-3,height-3);}}//定时器显示界面classClockCanvasextendsCanvasimplementsCommandListener{//定义定时器privateTimertimer;privateClockTimerTasktimerTask;privateCommandcmdBack=newCommand("返回",Command.EXIT,1);ClockCanvas(){timer=newTimer();timerTask=newClockTimerTask();timer.schedule(timerTask,1000,1000);//每一秒执行一次this.addCommand(cmdBack);this.setCommandListener(this);}publicvoidcommandAction(Commandc,Displayabled){if(c==cmdBack){mDisplay.setCurrent(mMainForm);}}/***在这个方法中绘制屏幕*/protectedvoidpaint(Graphicsg){//清除屏幕-背景黑色g.setColor(0x000000);g.fillRect(0,0,this.getWidth(),this.getHeight());g.setColor(0xff0000);Fontfont=Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE);g.setFont(font);g.drawString(getTimeStr(),getWidth()/2,getHeight()/2,Graphics.TOP|Graphics.HCENTER);}/***返回表示当前时间的字符串*/privateStringgetTimeStr(){Calendarcal=Calendar.getInstance();inthour_of_day=(int)cal.get(Calendar.HOUR_OF_DAY)+8;intminute=(int)cal.get(Calendar.MINUTE);intsecond=(int)cal.get(Calendar.SECOND);intyear=(int)cal.get(Calendar.YEAR);intmonth=(int)cal.get(Calendar.MONTH)+1;intday=(int)cal.get(Calendar.DAY_OF_MONTH);returnyear+"-"+month+"-"+day+""+hour_of_day+":"+minute+":"+second;}}}
解决方案
解决方案二:
没人回答,往上顶
解决方案三:
好长。顶了
解决方案四:
是不是文件扩展名和类型不一样呀
解决方案五:
System.getProperty("video.snapshot.encodings")==快照格式试一下吧~~
解决方案六:
问下楼主你这程序在哪个机子上能跑啊
解决方案七:
mVideoControl.getSnapshot("图片格式");应该是这么填的:Stringstr="encoding=jpeg&width=800&height=600";mVideoControl.getSnapshot(str);还可以填:Stringstr="encoding=pcm&width=800&height=600";
解决方案八:
packageprj1_1_3;importjavax.microedition.midlet.MIDlet;importjavax.microedition.midlet.MIDletStateChangeException;publicclassMidlet1extendsMIDlet{publicMidlet1(){super();}protectedvoiddestroyApp(booleanarg0)throwsMIDletStateChangeException{this.notifyDestroyed();}protectedvoidpauseApp(){this.notifyPaused();}protectedvoidstartApp()throwsMIDletStateChangeException{Stringprop=System.getProperty("audio.encodings");if(prop!=null){System.out.println("照相机支持的图片格式:n"+prop);}}}
MOTODEVSDKforJava(TM)ME3.x测试的结果是:encoding=audio/amrencoding=audio/amr-wbStringstr="encoding=audio/amr&width=800&height=600";Stringstr="encoding=audio/amr-wb&width=800&height=600";
解决方案九:
谢谢大家,已经解决了
解决方案十:
THX