【问题标题】:how can I resize the image with respect to black berry height and width如何根据黑莓的高度和宽度调整图像大小
【发布时间】:2011-06-15 05:36:11
【问题描述】:

如何根据黑莓屏幕的高度和宽度调整来自 HTTP 服务器的图像的大小?请有任何解决方案。

【问题讨论】:

    标签: blackberry


    【解决方案1】:

    使用参数 Disply.getWidth() 和 Display.getHieght() 试试这个。

    package CustomizedField;
    
    import net.rim.device.api.system.Bitmap;
    import net.rim.device.api.ui.Color;
    import net.rim.device.api.ui.Graphics;
    
    public class GPATools
    {
        /**
         * Resizes a bitmap with an alpha channel (transparency) 
         * without the artifacts introduced
         *   by <code>scaleInto()</code>.
         *
         * @param bmpSrc        Source Bitmap
         * @param nWidth        New Width
         * @param nHeight       New Height
         * @param nFilterType   Filter quality to use. Can be          
         *                           <code>Bitmap.FILTER_LANCZOS</code>,
         *                           <code>Bitmap.FILTER_BILINEAR</code> or
         *                           <code>Bitmap.FILTER_BOX</code>.
         * @param nAspectRatio  Specifies how the picture is resized. Can be
         *                           <code>Bitmap.SCALE_TO_FIT</code>,
         *                           <code>Bitmap.SCALE_TO_FILL</code> or
         *                           <code>Bitmap.SCALE_STRETCH</code>.
         * @return              The resized Bitmap in a new object.
         */
        public static Bitmap ResizeTransparentBitmap(Bitmap bmpSrc, 
         int nWidth, int nHeight, int nFilterType, int nAspectRatio)
        {
            if(bmpSrc== null)
                return null;
    
            //Get the original dimensions of the bitmap
            int nOriginWidth = bmpSrc.getWidth();
            int nOriginHeight = bmpSrc.getHeight();
            if(nWidth == nOriginWidth && nHeight == nOriginHeight)
                return bmpSrc;
    
            //Prepare a drawing bitmap and graphic object
            Bitmap bmpOrigin = new Bitmap(nOriginWidth, nOriginHeight);
            Graphics graph = Graphics.create(bmpOrigin);
    
            //Create a line of transparent pixels for later use
            int[] aEmptyLine = new int[nWidth];
            for(int x = 0; x < nWidth; x++)
                aEmptyLine[x] = 0x00000000;
            //Create two scaled bitmaps
            Bitmap[] bmpScaled = new Bitmap[2];
            for(int i = 0; i < 2; i++)
            {
                //Draw the bitmap on a white background first, 
                //then on a black background
                graph.setColor((i == 0) ? Color.WHITE : Color.BLACK);
                graph.fillRect(0, 0, nOriginWidth, nOriginHeight);
                graph.drawBitmap(0, 0, nOriginWidth, nOriginHeight, bmpSrc, 0, 0);
    
                //Create a new bitmap with the desired size
                bmpScaled[i] = new Bitmap(nWidth, nHeight);
                if(nAspectRatio == Bitmap.SCALE_TO_FIT)
                {
                    //Set the alpha channel of all pixels to 0 
                    //to ensure transparency is
                    //applied around the picture, if needed by the transformation
                    for(int y = 0; y < nHeight; y++)
                        bmpScaled[i].setARGB(aEmptyLine, 0, nWidth, 
                          0, y, nWidth, 1);
                }
    
                //Scale the bitmap
                bmpOrigin.scaleInto(bmpScaled[i], nFilterType, nAspectRatio);
            }
    
            //Prepare objects for final iteration
            Bitmap bmpFinal = bmpScaled[0];
            int[][] aPixelLine = new int[2][nWidth];
    
            //Iterate every line of the two scaled bitmaps
            for(int y = 0; y < nHeight; y++)
            {
                bmpScaled[0].getARGB(aPixelLine[0], 0, nWidth, 0, y, nWidth, 1);
                bmpScaled[1].getARGB(aPixelLine[1], 0, nWidth, 0, y, nWidth, 1);
    
                //Check every pixel one by one
                for(int x = 0; x < nWidth; x++)
                {
                    //If the pixel was untouched (alpha channel still at 0), 
                    //keep it transparent
                    if(((aPixelLine[0][x] >> 24) & 0xff) == 0)
                        aPixelLine[0][x] = 0x00000000;
                    else
                    {
                        //Compute the alpha value based on the difference 
                        //of intensity in the red channel
                        int nAlpha = ((aPixelLine[1][x] >> 16) & 0xff) -
                                        ((aPixelLine[0][x] >> 16) & 0xff) + 255;
                        if(nAlpha == 0)
                            aPixelLine[0][x] = 0x00000000; //Completely transparent
                        else if(nAlpha >= 255)
                            aPixelLine[0][x] |= 0xff000000; //Completely opaque
                        else
                        {
                            //Compute the value of the each channel one by one
                            int nRed = ((aPixelLine[0][x] >> 16 ) & 0xff);
                            int nGreen = ((aPixelLine[0][x] >> 8 ) & 0xff);
                            int nBlue = (aPixelLine[0][x] & 0xff);
    
                            nRed = (int)(255 + 
                              (255.0 * ((double)(nRed-255)/(double)nAlpha)));
                            nGreen = (int)(255 + 
                              (255.0 * ((double)(nGreen-255)/(double)nAlpha)));
                            nBlue = (int)(255 + 
                              (255.0 * ((double)(nBlue-255)/(double)nAlpha)));
    
                            if(nRed < 0) nRed = 0;
                            if(nGreen < 0) nGreen = 0;
                            if(nBlue < 0) nBlue = 0;
                            aPixelLine[0][x] = nBlue | (nGreen<<8) | 
                              (nRed<<16) | (nAlpha<<24);
                        }
                    }
                }
    
                //Change the pixels of this line to their final value
                bmpFinal.setARGB(aPixelLine[0], 0, nWidth, 0, y, nWidth, 1);
            }
            return bmpFinal;
        }
    } 
    

    【讨论】:

      【解决方案2】:

      从图像的字节创建一个EncodedImage

      使用Display.getHeight()Display.getWidth() 获取显示器的尺寸。

      然后使用EncodedImage.scaleImage32() 调整大小。检查this post 了解具体情况。

      【讨论】:

        【解决方案3】:

        这是将图像调整为任意大小的方法:

        public  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);
                    }
        

        您只需传递宽度和高度 - 您想要调整图像大小的参数。

        您可以分别使用Display.getWidth()Display.getHeight() 获得显示屏幕的宽度和高度。

        【讨论】:

          猜你喜欢
          • 2017-12-08
          • 2023-03-22
          • 1970-01-01
          • 1970-01-01
          • 2021-12-28
          • 1970-01-01
          • 2013-04-28
          • 1970-01-01
          • 2014-04-04
          相关资源
          最近更新 更多