⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imageutil.java

📁 j2me简单实例,j2me教程加源码,希望大家喜欢
💻 JAVA
字号:
package com.j2medev.chapter3;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class ImageUtil {
    
    public static final int ROTATE_MIRROR = 0;
    public static final int ROTATE_180 = 1;
    //创建缩略图
    public static Image createThumbnail(Image image,int width) {
        int sourceWidth = image.getWidth();
        int sourceHeight = image.getHeight();
        
        int thumbWidth = width;
        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;
    }
    
    //从int[]数组转换为byte[]
    private byte[] int2byte(int[] data) {
        byte[] buffer = new byte[4*data.length];
        for(int i=0; i<data.length; i++) {
            int p = data[i];
            buffer[i*4+0] = (byte)((p & 0xff000000)>>>24);
            buffer[i*4+1] = (byte)((p & 0xff0000)>>>16);
            buffer[i*4+2] = (byte)((p & 0xff00)>>>8);
            buffer[i*4+3] = (byte)((p & 0xff));
        }
        return buffer;
    }
    //从byte[]转换为int[]
    private int[] byte2int(byte[] data) {
        int[] buffer = new int[data.length/4];
        for(int i=0; i<data.length/4; i++) {
            int a = 0xff & data[i*4+0];
            int r = 0xff & data[i*4+1];
            int g = 0xff & data[i*4+2];
            int b = 0xff & data[i*4+3];
            buffer[i] = (a << 24) | (r << 16) | (g << 8) | b;
        }
        return buffer;
    }
    
    static private int[] reescaleArray(int[] ini, int x, int y, int x2, int y2) {
        //抽样算法
        int out[] = new int[x2*y2];
        for (int yy = 0;  yy < y2; yy++) {
            int dy = yy * y / y2;
            for (int xx = 0; xx < x2; xx++) {
                int dx = xx * x / x2;
                out[(x2*yy)+xx]=ini[(x*dy)+dx];
            }
        }
        return out;
    }
    
    public static Image scaleImage(Image src, int newWidth, int newHeight){
        int tX = src.getWidth();
        int tY = src.getHeight();
        int  rgb[] = new int[tX*tY];
        //将源图的像素数据存储在rgb数组中
        src.getRGB(rgb,0,tX,0,0,tX,tY);
        //生成新的图片像素数据
        int rgb2[] = reescaleArray(rgb,tX,tY,newWidth,newHeight);
        rgb = null;
        Image temp2 = Image.createRGBImage(rgb2,newWidth,newHeight,true);
        rgb2 = null;
        return temp2;
    }
    
    public static Image rotateImage(Image img,int mode){
        if(mode>1 || mode <0){
            throw new IllegalArgumentException("Wrong rotate mode value");
        }
        int w = img.getWidth();
        int h = img.getHeight();
        Image _img = null;
        if(mode == ROTATE_180){
            //水平翻转,新图片与老图片大小一样
            _img = Image.createImage(w,h);
            Graphics g = _img.getGraphics();
            for(int i = 0;i<h;i++){
                g.setClip(0,i,w,1);
                g.translate(0,-h+i*2);
                g.drawImage(img,0,0,Graphics.LEFT|Graphics.TOP);
                g.translate(0-g.getTranslateX(),0-g.getTranslateY());
            }
        }else{
            //垂直翻转,新图片和老图片高度和宽度互换
            _img = Image.createImage(img.getHeight(),img.getWidth());
            Graphics g = _img.getGraphics();
            for(int j = 0;j<w;j++){
                g.setClip(j,0,1,h);
                g.translate(-w+2*j,0);
                g.drawImage(img,0,0,Graphics.LEFT|Graphics.TOP);
                g.translate(0-g.getTranslateX(),0-g.getTranslateY());
            }
        }
        return _img;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -