【问题标题】:How to Set Image height and width in j2me(java)如何在 j2me(java) 中设置图像的高度和宽度
【发布时间】:2012-08-04 07:14:17
【问题描述】:

我已经从图像 url(lcdui 图像)创建了一个图像

HttpConnection c = (HttpConnection) Connector.open(imageurl);
int len = (int)c.getLength();

if (len > 0) 
{
is = c.openDataInputStream();
byte[] data = new byte[len];
is.readFully(data);

img = Image.createImage(data, 0, len);

我想为此设置高度和宽度?我想显示

【问题讨论】:

    标签: java-me lcdui


    【解决方案1】:

    您不需要设置宽度和高度,因为在图像加载期间会加载并设置此信息。因此,如果图像是 320x100,您的代码将创建 320x100 图像。 img.getWidth() 将返回 320。img.getHeight() 将返回 100。

    无法更改Image 对象的宽度和高度。您可以只查询它的宽度和高度。

    您的图像已准备好呈现在画布中的ImageItem 对象中。

    【讨论】:

      【解决方案2】:

      无法将宽度和高度设置为 Image。但是,您可以使用以下方法调整图像大小

      public Image resizeImage(Image src, int screenHeight, int screenWidth) {
              int srcWidth = src.getWidth();
      
              int srcHeight = src.getHeight();
              Image tmp = Image.createImage(screenWidth, srcHeight);
              Graphics g = tmp.getGraphics();
              int ratio = (srcWidth << 16) / screenWidth;
              int pos = ratio / 2;
      
              //Horizontal Resize        
      
              for (int index = 0; index < screenWidth; index++) {
                  g.setClip(index, 0, 1, srcHeight);
                  g.drawImage(src, index - (pos >> 16), 0);
                  pos += ratio;
              }
      
              Image resizedImage = Image.createImage(screenWidth, screenHeight);
              g = resizedImage.getGraphics();
              ratio = (srcHeight << 16) / screenHeight;
              pos = ratio / 2;
      
              //Vertical resize
      
              for (int index = 0; index < screenHeight; index++) {
                  g.setClip(0, index, screenWidth, 1);
                  g.drawImage(tmp, 0, index - (pos >> 16));
                  pos += ratio;
              }
              return resizedImage;
      
          }
      

      【讨论】:

      【解决方案3】:

      接受的答案对我不起作用(因为在减小图像尺寸时它会在图像底部留下一条白带 - 尽管保持相同的纵横比)。我在CodeRanch forum 中找到了一个可以使用的 sn-p 代码。

      这是sn-p,已清理:

      protected static Image resizeImage(Image image, int resizedWidth, int resizedHeight) {
      
          int width = image.getWidth();
          int height = image.getHeight();
      
          int[] in = new int[width];
          int[] out = new int[resizedWidth * resizedHeight];
      
          int dy, dx;
          for (int y = 0; y < resizedHeight; y++) {
      
              dy = y * height / resizedHeight;
              image.getRGB(in, 0, width, 0, dy, width, 1);
      
              for (int x = 0; x < resizedWidth; x++) {
                  dx = x * width / resizedWidth;
                  out[(resizedWidth * y) + x] = in[dx];
              }
      
          }
      
          return Image.createRGBImage(out, resizedWidth, resizedHeight, true);
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-01
        • 2014-06-21
        • 2012-09-05
        • 2019-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多