📄 takepicture.java
字号:
package barcodereader;import java.io.IOException;import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.Image;import javax.microedition.media.Manager;import javax.microedition.media.MediaException;import javax.microedition.media.Player;import javax.microedition.media.control.VideoControl;public class TakePicture extends Canvas implements Runnable{ private BarcodeReader barcodereader; private Image background, bottommenu; private Player player; private VideoControl videocontrol; private boolean viewcameracondition, condition, takepicturecondition; public TakePicture(BarcodeReader br) { barcodereader = br; setFullScreenMode(true); try{ background = Image.createImage(getClass().getResourceAsStream( "/image/takepictureresultbackground.PNG")); bottommenu = Image.createImage(getClass().getResourceAsStream( "/image/takepicturebottommenu.PNG")); } catch(IOException ioe) { ioe.printStackTrace(); } viewcameracondition = false; condition = true; takepicturecondition = false; } public void paint(Graphics g) { barcodereader.graphics.setColor(0, 0, 0); barcodereader.graphics.fillRect(0, 0, barcodereader.bufferheight, barcodereader.bufferwidth); barcodereader.graphics.drawImage(background, 0, 0, Graphics.LEFT | Graphics.TOP); barcodereader.graphics.drawImage(bottommenu, 0, 200, Graphics.LEFT | Graphics.TOP); g.setColor(0, 0, 0); g.fillRect(0, 0, barcodereader.width, barcodereader.height); g.drawImage(barcodereader.background, barcodereader.halfwidth, barcodereader.halfheight, Graphics.HCENTER | Graphics.VCENTER); g.drawImage(barcodereader.buffer, barcodereader.halfwidth, barcodereader.halfheight, Graphics.HCENTER | Graphics.VCENTER); } public void run() { byte[] imagebyte; if(!viewcameracondition) { viewCamera(); viewcameracondition = true; } else{ barcodereader.display.setCurrent(this); } condition = true; takepicturecondition = false; while(condition) { } if(takepicturecondition) { try{ imagebyte = videocontrol.getSnapshot(null); barcodereader.mainmenu.pictureprocess.processor.image = Image.createImage(imagebyte, 0, imagebyte.length); barcodereader.mainmenu.pictureprocess.processor.image = changeImageScale( barcodereader.mainmenu.pictureprocess.processor.image, 120, 160); //debug /*barcodereader.mainmenu.pictureprocess.processor.image = Image.createImage(getClass().getResourceAsStream("/image/image15.PNG")); barcodereader.mainmenu.pictureprocess.processor.image = changeImageScale(barcodereader.mainmenu.pictureprocess.processor.image, 120, 160);*/ barcodereader.mainmenu.pictureview.setFullScreenMode(true); barcodereader.display.setCurrent( barcodereader.mainmenu.pictureview); } catch(MediaException me) { me.printStackTrace(); } //debug /*catch(IOException ioe) { ioe.printStackTrace(); }*/ takepicturecondition = false; } } public void keyPressed(int keyCode) { if(keyCode==49) { barcodereader.mainmenu.backFromTakePicture(); } else if((keyCode==-5)||(keyCode==53)) { condition = false; takepicturecondition = true; } } public void viewCamera() { try{ player = Manager.createPlayer("capture://video"); player.realize(); if((videocontrol=(VideoControl)player.getControl("VideoControl"))!=null) { videocontrol.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this); videocontrol.setDisplaySize(120, 160); videocontrol.setDisplayLocation((barcodereader.width-120)/2, ((barcodereader.height-(160+20))/2)+barcodereader.heightoffset); videocontrol.setVisible(true); } player.start(); barcodereader.display.setCurrent(this); } catch(IOException ioe) { ioe.printStackTrace(); } catch(MediaException me) { me.printStackTrace(); } } public Image changeImageScale(Image oi, int nw, int nh) { Image oldimage; int newwidth, newheight, oldimagewidth, oldimageheight, x1, y1, x2, y2, oldimageoffset, newimageoffset; int[] oldimagedata, newimagedata; oldimage = oi; newwidth = nw; newheight = nh; oldimagewidth = oldimage.getWidth(); oldimageheight = oldimage.getHeight(); oldimagedata = new int[oldimagewidth*oldimageheight]; oldimage.getRGB(oldimagedata, 0, oldimagewidth, 0, 0, oldimagewidth, oldimageheight); newimagedata = new int[newwidth*newheight]; x1 = oldimagewidth/newwidth; x2 = oldimagewidth%newwidth; y1 = (oldimageheight/newheight)*oldimagewidth-oldimagewidth; y2 = oldimageheight%newheight; oldimageoffset = 0; newimageoffset = 0; for(int y=newheight, y3=0;y>0;y--) { for(int x=newwidth, x3=0;x>0;x--) { newimagedata[newimageoffset++] = oldimagedata[oldimageoffset]; oldimageoffset += x1; x3 += x2; if(x3>=newwidth) { x3 -= newwidth; oldimageoffset++; } } oldimageoffset += y1; y3 += y2; if(y3>=newheight) { y3 -= newheight; oldimageoffset += oldimagewidth; } } return Image.createRGBImage(newimagedata, newwidth, newheight, false); } public void quit() { condition = false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -