📄 camera.java
字号:
/* * Copyright (C) 2004 MobileBlogger Development Team */package net.sourceforge.mobileblogger;import java.io.IOException;import javax.microedition.lcdui.*;import javax.microedition.media.*;import javax.microedition.media.control.*;import javax.microedition.midlet.*;public class Camera extends Form implements CommandListener { private MIDlet midlet; private Display display; private Command mExitCommand, mCameraCommand; private Command mBackCommand, mCaptureCommand; private Player mPlayer; private VideoControl mVideoControl; /*********************/ public Camera(MIDlet m, Display d) { super("Photo Blogging"); midlet = m; display = d; mExitCommand = new Command("Exit", Command.EXIT, 0); mCameraCommand = new Command("Camera", Command.SCREEN, 0); mBackCommand = new Command("Back", Command.BACK, 0); mCaptureCommand = new Command("Capture", Command.SCREEN, 0); addCommand(mBackCommand); String supports = System.getProperty("video.snapshot.encodings"); if (supports != null && supports.length() > 0) { append("Ready to take pictures."); addCommand(mCameraCommand); } else append("Mobile Blogger cannot use this " + "device to take pictures."); setCommandListener(this); } /*********************/ public void commandAction(Command c, Displayable s) { if (c.getCommandType() == Command.EXIT) { display.setCurrent( new Menu(midlet, display) ); } else if (c == mCameraCommand) showCamera(); else if (c == mBackCommand) display.setCurrent( new Menu(midlet, display) ); else if (c == mCaptureCommand) { capture(); } } /******************/ private void showCamera() { try { mPlayer = Manager.createPlayer("capture://video"); mPlayer.realize(); mVideoControl = (VideoControl)mPlayer.getControl("VideoControl"); Canvas canvas = new CameraCanvas(midlet, mVideoControl, this); canvas.addCommand(mBackCommand); canvas.addCommand(mCaptureCommand); canvas.setCommandListener(this); display.setCurrent(canvas); mPlayer.start(); } catch (IOException ioe) { handleException(ioe); } catch (MediaException me) { handleException(me); } } /*******************/ public void capture() { try { // Get the image. byte[] raw = mVideoControl.getSnapshot(null); Image image = Image.createImage(raw, 0, raw.length); Image thumb = createThumbnail(image); // Place it in the main form. if (size() > 0 && get(0) instanceof StringItem) delete(0); append(thumb); // Flip back to the main form. display.setCurrent(this); // Shut down the player. mPlayer.close(); mPlayer = null; mVideoControl = null; } catch (MediaException me) { handleException(me); } } private void handleException(Exception e) { Alert a = new Alert("Exception", e.toString(), null, null); a.setTimeout(50); append("Mobile Blogger cannot use this " + "device to take pictures. Press Back."); //display.setCurrent( new Menu(midlet, display) ); } /********************/ private Image createThumbnail(Image image) { int sourceWidth = image.getWidth(); int sourceHeight = image.getHeight(); int thumbWidth = 64; int thumbHeight = -1; if (thumbHeight == -1) thumbHeight = thumbWidth * sourceHeight / sourceWidth; Image thumb = Image.createImage(thumbWidth, thumbHeight); Graphics g = thumb.getGraphics(); for (int y = 0; y < thumbHeight; y++) { for (int x = 0; x < thumbWidth; x++) { g.setClip(x, y, 1, 1); int dx = x * sourceWidth / thumbWidth; int dy = y * sourceHeight / thumbHeight; g.drawImage(image, x - dx, y - dy, Graphics.LEFT | Graphics.TOP); } } Image immutableThumb = Image.createImage(thumb); return immutableThumb; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -