带有缩放功能的图片查看器

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;

public class Exec extends MIDlet {
    private Display display;

    public Exec() {
        display = Display.getDisplay(this);
    }

    public void startApp() {

        display.setCurrent(new DrawPanel());
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}

class DrawPanel extends Canvas implements CommandListener {
    final int step = 3;
    final int zoomRatio = 10;
    int currentX = 0;
    int currentY = 0;
    Image image;
    Image originalImage;
    Command command = new Command("恢复", Command.ITEM, 1);

    public DrawPanel() {
        image = getImage("/images/java.jpg");
        originalImage = image;
        addCommand(command);
        setCommandListener(this);
    }

    public void paint(Graphics g) {
        g.setColor(0, 0, 0);
        g.fillRect(0, 0, getWidth(), getHeight());
        int imageWidth = image.getWidth();
        int imageHeight = image.getHeight();
        g.drawImage(image, getWidth() / 2 - imageWidth / 2 - currentX,
                getHeight() / 2 - imageHeight / 2 - currentY, Graphics.TOP
                        | Graphics.LEFT);
    }

    public Image getImage(String imagePath) {
        Image image = null;
        try {
            image = Image.createImage(imagePath);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return image;
    }

    public void commandAction(Command com, Displayable disp) {
        currentX = 0;
        currentY = 0;
        image = originalImage;
        repaint();
    }

    protected void keyPressed(int keyCode) {
        switch (getGameAction(keyCode)) {
        case Canvas.UP:
            currentY -= step;
            break;
        case Canvas.DOWN:
            currentY += step;
            break;
        case Canvas.LEFT:
            currentX -= step;
            break;
        case Canvas.RIGHT:
            currentX += step;
            break;
        case Canvas.FIRE:
            image = ZoomImage(originalImage, image.getWidth() + zoomRatio, image
                    .getHeight()
                    + zoomRatio);
            break;
        }
        repaint();
    }

    protected void keyRepeated(int keyCode) {
        keyPressed(keyCode);
    }

    public Image ZoomImage(Image src, int desW, int desH) {
        Image desImg = null;
        int srcW = src.getWidth(); // 原始图像宽
        int srcH = src.getHeight(); // 原始图像高
        int[] srcBuf = new int[srcW * srcH]; // 原始图片像素信息缓存

        src.getRGB(srcBuf, 0, srcW, 0, 0, srcW, srcH);

        // 计算插值表
        int[] tabY = new int[desH];
        int[] tabX = new int[desW];

        int sb = 0;
        int db = 0;
        int tems = 0;
        int temd = 0;
        int distance = srcH > desH ? srcH : desH;
        for (int i = 0; i <= distance; i++) { /* 垂直方向 */
            tabY[db] = sb;
            tems += srcH;
            temd += desH;
            if (tems > distance) {
                tems -= distance;
                sb++;
            }
            if (temd > distance) {
                temd -= distance;
                db++;
            }
        }

        sb = 0;
        db = 0;
        tems = 0;
        temd = 0;
        distance = srcW > desW ? srcW : desW;
        for (int i = 0; i <= distance; i++) { /* 水平方向 */
            tabX[db] = (short) sb;
            tems += srcW;
            temd += desW;
            if (tems > distance) {
                tems -= distance;
                sb++;
            }
            if (temd > distance) {
                temd -= distance;
                db++;
            }
        }

        // 生成放大缩小后图形像素buf
        int[] desBuf = new int[desW * desH];
        int dx = 0;
        int dy = 0;
        int sy = 0;
        int oldy = -1;
        for (int i = 0; i < desH; i++) {
            if (oldy == tabY[i]) {
                System.arraycopy(desBuf, dy - desW, desBuf, dy, desW);
            } else {
                dx = 0;
                for (int j = 0; j < desW; j++) {
                    desBuf[dy + dx] = srcBuf[sy + tabX[j]];
                    dx++;
                }
                sy += (tabY[i] - oldy) * srcW;
            }
            oldy = tabY[i];
            dy += desW;
        }

        // 生成图片
        desImg = Image.createRGBImage(desBuf, desW, desH, false);
        return desImg;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leeshuqing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值