当用户用他的手机拍照时,我希望 LWUIT 获取一张特定的照片以添加到记录存储中,然后再检索该照片。如何做到这一点?
user768004
问问题
328 次
2 回答
1
好的,我让应用程序通过命令启动相机:
mPlayer = Manager.createPlayer("capture://video");
mPlayer.realize();
mVideoControl = (VideoControl) mPlayer.getControl("VideoControl");
Canvas canvas = new CameraCanvas(this, mVideoControl);
canvas.addCommand(mBackCommand);
canvas.addCommand(mCaptureCommand);
canvas.setCommandListener(this);
mDisplay.setCurrent(canvas);
mPlayer.start();
在 mCaptureCommand 命令的 actionPerformed 中:
public void capture() {
try {
// Get the image.
byte[] raw = mVideoControl.getSnapshot(null);
// "encoding=png&width=320&height=240");
bytelen = raw.length;
Image image = Image.createImage(raw, 0, raw.length);
Image thumb = createThumbnail(image);
// Place it in the main form.
if (mMainForm.size() > 0 && mMainForm.get(0) instanceof StringItem) {
mMainForm.delete(0);
}
mMainForm.append(thumb);
// Flip back to the main form.
mDisplay.setCurrent(mMainForm);
// Shut down the player.
mPlayer.close();
mPlayer = null;
mVideoControl = null;
} catch (MediaException me) {
handleException(me);
}
}
createThumbnail 的代码:
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;
}
现在我不知道调用 createThumbnail 方法时 Image 存储在哪里,即在调用 Image.createImage 之后:有两个 createImage 调用,一个在 capture() 方法中,一个在 createThumbnail() 方法中。但我真正的问题是要知道创建的图像的位置以及如何将它与银行客户的记录存储 ID 相关联。
于 2011-06-16T08:51:19.423 回答