【问题标题】:Blackberry - how to resize image?黑莓 - 如何调整图像大小?
【发布时间】:2010-12-18 16:35:58
【问题描述】:

我想知道我们是否可以调整图像的大小。假设我们想在我们的黑莓屏幕上绘制一个实际大小为 200x200 的图像,大小为 100 x 100。

谢谢

【问题讨论】:

    标签: graphics blackberry drawing custom-controls image-scaling


    【解决方案1】:

    我不是黑莓程序员,但我相信其中一些链接会对您有所帮助:

    Image Resizing Article
    Resizing a Bitmap on the Blackberry
    Blackberry Image Scaling Question

    【讨论】:

      【解决方案2】:
      【解决方案3】:

      请记住,BlackBerry 所做的默认图像缩放非常原始,通常看起来不太好。如果您正在为 5.0 构建,则可以使用 new API 使用双线性或 Lanczos 等过滤器进行更好的图像缩放。

      【讨论】:

        【解决方案4】:

        您可以使用EncodedImage.scaleImage32() 方法非常简单地做到这一点。您需要向它提供要缩放宽度和高度的因子(作为Fixed32)。

        这里有一些示例代码,它使用 RIM 的 Fixed32 类,通过将原始图像大小除以所需大小来确定宽度和高度的比例因子。

        public static EncodedImage resizeImage(EncodedImage image, int newWidth, int newHeight) {
            int scaleFactorX = Fixed32.div(Fixed32.toFP(image.getWidth()), Fixed32.toFP(newWidth));
            int scaleFactorY = Fixed32.div(Fixed32.toFP(image.getHeight()), Fixed32.toFP(newHeight));
            return image.scaleImage32(scaleFactorX, scaleFactorY);
        }
        

        如果您有幸成为 OS 5.0 的开发人员,Marc 发布了一个指向 new APIs 的链接,该链接比我上面描述的更清晰、更通用。例如:

        public static Bitmap resizeImage(Bitmap originalImage, int newWidth, int newHeight) {
            Bitmap newImage = new Bitmap(newWidth, newHeight);
            originalImage.scaleInto(newImage, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FILL);
            return newImage;
        }
        

        (当然,您可以根据需要替换过滤器/缩放选项。)

        【讨论】:

        • @TreeUK:我现在在工作代码中有这个确切的功能。您是否将ints 转换为Fixed32 并使用Fixed32.div() 来计算比例因子?正常的整数除法不会削减它。
        • 谢谢,使用不正确。与整数无关,但仍然是错误的。
        【解决方案5】:

        对于 BlackBerry JDE 5.0 或更高版本,您可以使用 scaleInto API。

        【讨论】:

          【解决方案6】:

          这里是函数,或者你可以说调整图像大小的方法,随意使用它:

          int olddWidth;
          int olddHeight;
          int dispplayWidth;
          int dispplayHeight;
          
          EncodedImage ei2 = EncodedImage.getEncodedImageResource("add2.png");
          olddWidth = ei2.getWidth();
          olddHeight = ei2.getHeight();
          dispplayWidth = 40;\\here pass the width u want in pixels
          dispplayHeight = 80;\\here pass the height u want in pixels again
          
          int numeerator = net.rim.device.api.math.Fixed32.toFP(olddWidth);
          int denoominator = net.rim.device.api.math.Fixed32.toFP(dispplayWidth);
          int widtthScale = net.rim.device.api.math.Fixed32.div(numeerator, denoominator);
          numeerator = net.rim.device.api.math.Fixed32.toFP(olddHeight);
          denoominator = net.rim.device.api.math.Fixed32.toFP(dispplayHeight);
          int heighhtScale = net.rim.device.api.math.Fixed32.div(numeerator, denoominator);
          EncodedImage newEi2 = ei2.scaleImage32(widtthScale, heighhtScale); 
          Bitmap _add =newEi2.getBitmap();
          

          【讨论】:

            【解决方案7】:
            in this there is two bitmap.temp is holding the old bitmap.In this method you just pass
            bitmap ,width,height.it return new bitmap of your choice.
            
              Bitmap ImgResizer(Bitmap bitmap , int width , int height){
                Bitmap temp=new Bitmap(width,height);
                Bitmap resized_Bitmap = bitmap;
                temp.createAlpha(Bitmap.HOURGLASS);
                resized_Bitmap.scaleInto(temp , Bitmap.FILTER_LANCZOS);
                return temp;
            }
            

            【讨论】:

            • 只需调用此方法并将其保存在位图中..享受
            • scaleInto() for Bitmap 自 OS 5.0 起可用。
            • @Lucas 是的,从 BlackBerry API 5.0.0 开始就是这样
            【解决方案8】:

            我正在为黑莓应用程序开发中的新手发布此答案。下面的代码用于处理来自 URL 的位图图像并在不丢失纵横比的情况下调整它们的大小:

               public static Bitmap imageFromServer(String url)
            {
            Bitmap bitmp = null;
            try{
            HttpConnection fcon = (HttpConnection)Connector.open(url);
            int rc = fcon.getResponseCode();
            if(rc!=HttpConnection.HTTP_OK)
            {
                throw new IOException("Http Response Code : " + rc);            
            }
            InputStream httpInput = fcon.openDataInputStream();
            InputStream inp = httpInput;
            byte[] b = IOUtilities.streamToBytes(inp);
            EncodedImage img = EncodedImage.createEncodedImage(b, 0, b.length);
            bitmp = resizeImage(img.getBitmap(), 100, 100);
            }
            catch(Exception e)
            {
            Dialog.alert("Exception : " + e.getMessage());          
            }
            return bitmp;
            }
            
            public static Bitmap resizeImage(Bitmap originalImg, int newWidth, int newHeight)
            {
                Bitmap scaledImage = new Bitmap(newWidth, newHeight);
                originalImg.scaleInto(scaledImage, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FIT);
                return scaledImage;
            }
            

            方法 resizeImage 在方法 imageFromServer(String url) 中被调用。 1) 使用 EncodedImage img 处理来自服务器的图像。 2) 位图bitmp = resizeImage(img.getBitmap(), 100, 100); 参数被传递给 resizeImage(),resizeImage() 的返回值被设置为 Bitmap bitmp。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2014-06-03
              • 1970-01-01
              • 2012-07-14
              • 2013-12-24
              • 1970-01-01
              • 2020-10-02
              相关资源
              最近更新 更多