gzfxxy
路人甲
路人甲
  • 注册日期2006-07-19
  • 发帖数11
  • QQ
  • 铜币137枚
  • 威望0点
  • 贡献值0点
  • 银元0个
阅读:6618回复:11

Java applet实现图片的放缩问题

楼主#
更多 发布于:2006-08-10 09:14
<P>现总结两种方法:</P>
<P>1.在画板(canvas)中显示图片,通过画板大小的控制来实现图片的放大缩小.</P>
<P>2.直接利用算法来控制图片的放大缩小:</P>
<P>(1)获取Image图片像素信息:<BR>标准的midp1.0没有提供获取图片像素信息的函数,对于NOKIA的机器,我们可以采用Nokia SDK提供的API获取像素信息。具体程序如下:<BR>g = image.getGraphics()<BR>DirectGraphics dg = DirectUtils.getDirectGraphics(g);<BR>dg.getPixels(short[] pixels, int offset, int scanlength, int x,int y, int width, int height, int format)<BR>参数介绍:<BR>short[] pixels: 用于接收像素信息的数组<BR>int offset:这篇文章中的用到的地方,添0就可以了<BR>int scanlength:添图片的宽度就行了<BR>int x:添0<BR>int y:添0<BR>int width:图片宽度<BR>int height:图片高度<BR>int format:444,表示图形格式,好像Nokia S40的机器都是采用444格式表示RGB颜色的。就是红,绿,蓝各用4位表示,至于可以表示透明色ARGB的4444格式,应该是机器硬件实现的。<BR>想具体了解Nokia SDK的信息,可以查看Nokia SDK的帮助文档。</P>
<P>使用像素信息数组生成Image图片:<BR>image = Image.createImage(w, h);<BR>g = image.getGraphics()<BR>DirectGraphics dg = DirectUtils.getDirectGraphics(g);<BR>dg.drawPixels(short[] pixels,boolean transparency, int offset, int scanlength, int x, int y, int width,int height, int manipulation, int format)<BR>short[] pixels:像素信息数组<BR>boolean transparency:是否包含alpha位信息<BR>int offset:添 0<BR>int scanlength:添图片的宽度就行了<BR>int x:添 0<BR>int y:添 0<BR>int width:图片宽度<BR>int height:图片高度<BR>int manipulation:添 0<BR>int format:444</P>
<P>下面开始介绍具体的算法,首先给出图像缩放的完整函数,然后对代码,分段进行解释<BR>  /*********************************<BR>   * @todo 图片放大缩小<BR>   * @param srcImg 原始图片<BR>   * @param desW 变化后图片的宽<BR>   * @param desH 变化后图片的高<BR>   * @return 处理后的图片<BR>   *********************************/<BR>  private Image ZoomImage(Image srcImg, int desW, int desH) {<BR>    int srcW = srcImg.getWidth(); //原始图像宽<BR>    int srcH = srcImg.getHeight(); //原始图像高</P>
<P>    short[] srcBuf = new short[srcW * srcH]; //原始图片像素信息缓存</P>
<P>    //srcBuf获取图片像素信息<BR>    Image desImg = Image.createImage(srcW, srcH);<BR>    if (srcImg.isMutable()) { /*如果是可变图像*/<BR>      DirectUtils.getDirectGraphics(srcImg.getGraphics()).<BR>          getPixels(srcBuf, 0, srcW, 0, 0, srcW, srcH, 444);<BR>    } else { /*如果是非可变图像*/<BR>      desImg.getGraphics().drawImage(srcImg, 0, 0, 0);<BR>      DirectUtils.getDirectGraphics(desImg.getGraphics()).<BR>          getPixels(srcBuf, 0, srcW, 0, 0, srcW, srcH, 444);<BR>    }</P>
<P>    //计算插值表<BR>    short[] tabY = new short[desH];<BR>    short[] tabX = new short[desW];</P>
<P>    int sb = 0;<BR>    int db = 0;<BR>    int tems = 0;<BR>    int temd = 0;<BR>    int distance = srcH > desH ? srcH : desH;<BR>    for (int i = 0; i <= distance; i++) { /*垂直方向*/<BR>      tabY[db] = (short) sb;<BR>      tems += srcH;<BR>      temd += desH;<BR>      if (tems > distance) {<BR>        tems -= distance;<BR>        sb++;<BR>      }<BR>      if (temd > distance) {<BR>        temd -= distance;<BR>        db++;<BR>      }<BR>    }</P>
<P>    sb = 0;<BR>    db = 0;<BR>    tems = 0;<BR>    temd = 0;<BR>    distance = srcW > desW ? srcW : desW;<BR>    for (int i = 0; i <= distance; i++) { /*水平方向*/<BR>      tabX[db] = (short) sb;<BR>      tems += srcW;<BR>      temd += desW;<BR>      if (tems > distance) {<BR>        tems -= distance;<BR>        sb++;<BR>      }<BR>      if (temd > distance) {<BR>        temd -= distance;<BR>        db++;<BR>      }<BR>    }</P>
<P>    //生成放大缩小后图形像素buf<BR>    short[] desBuf = new short[desW * desH];<BR>    int dx = 0;<BR>    int dy = 0;<BR>    int sx = 0;<BR>    int sy = 0;<BR>    int oldy = -1;<BR>    for (int i = 0; i < desH; i++) {<BR>      if (oldy == tabY) {<BR>        System.arraycopy(desBuf, dy - desW, desBuf, dy, desW);<BR>      } else {<BR>        dx = 0;<BR>        for (int j = 0; j < desW; j++) {<BR>          desBuf[dy + dx] = srcBuf[sy + tabX[j]];<BR>          dx++;<BR>        }<BR>        sy += (tabY - oldy) * srcW;<BR>      }<BR>      oldy = tabY;<BR>      dy += desW;<BR>    }</P>
<P>    //生成图片<BR>    desImg = Image.createImage(desW, desH);<BR>    DirectUtils.getDirectGraphics(desImg.getGraphics()).<BR>        drawPixels(desBuf, true, 0, desW, 0, 0, desW, desH, 0, 444);<BR>    return desImg;<BR>  }</P>
<P>另外还有其他算法,若需要可直接与我联系:E_gzfplease@sian.com</P>
喜欢0 评分0
游客

返回顶部