BlackBerry平台图片缩放功能-Bitmap

BlackBerry 5.0 SDK引入了Bitmap类来支持图片缩放功能,提供不同滤波类型如Lanczos、Box和Bilinear。Lanczos算法虽然最慢但图像质量最佳,Box滤波器快速但结果模糊,Bilinear滤波器适用于图像大小调整,适合预览渲染。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

BlackBerry 5.0 SDK支持图片缩放,类Bitmap提供了对图片缩放的支持。

 

1. Bitmap.scaleInto(Bitmap dst, int filterType)

public void scaleInto(Bitmap dst,  int filterType)

          把原来的图片缩放成Bitmap dst的比例。

Parameters:
dst - Destination bitmap.
filterType - Interpolation filter type. May be one of:
Throws:
NullPointerException - Thrown if 'dst' is null.
IllegalArgumentException - Thrown if the destination bitmap is read-only.
IllegalArgumentException - Thrown if illegal filter type is specified.

Bitmap Scaling

各种图片缩放范例:

       testBitmap0 = Bitmap.getBitmapResource("rim.png");
       // Create different scaled versions of the original bitmap
       Bitmap scaledBitmap1 = new Bitmap(200, 50);
       Bitmap scaledBitmap2 = new Bitmap(200, 75);
       Bitmap scaledBitmap3 = new Bitmap(40, 60);
       Bitmap scaledBitmap4 = new Bitmap(200, 50);
       Bitmap scaledBitmap5 = new Bitmap(40, 60);
       Bitmap scaledBitmap6 = new Bitmap(200, 40);

      // Example 1. Scale the source bitmap and store it in another bitmap
      testBitmap0.scaleInto(scaledBitmap1, Bitmap.FILTER_BILINEAR);

      // Example 2. Copy fragments of the original bitmap into fragments of another bitmap
      testBitmap0.scaleInto(0, 0, testBitmap0.getWidth(), testBitmap0.getHeight(), scaledBitmap2,
              0, 0, 50, 50, Bitmap.FILTER_BILINEAR);
      testBitmap0.scaleInto( 0, 0, 50, 50, scaledBitmap2, 75, 15, 50, 50, Bitmap.FILTER_BILINEAR);
      testBitmap0.scaleInto(50, 0, 50, 50, scaledBitmap2, 150, 0, 50, 50, Bitmap.FILTER_BILINEAR);

      // Example 3. Scale preserving the aspect ratio. Fit horizontally.
      testBitmap0.scaleInto(scaledBitmap3, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FIT);

      // Example 4. Scale preserving the aspect ratio. Fit vertically.
      testBitmap0.scaleInto(scaledBitmap4, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FIT);

      // Example 5. Scale preserving the aspect ratio. Fill vertically.
       testBitmap0.scaleInto(scaledBitmap5, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FILL);

      // Example 6. Scale preserving the aspect ratio. Fill horizontally.
       testBitmap0.scaleInto(scaledBitmap6, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FILL);
各种图片缩放结果如下:
 

过滤参数包括:
  • Bitmap.FILTER_LANCZOS - Lanczos interpolation is considered to give best image quality, but it is the slowest out the other three interpolation algorithms. Lanczos interpolation produces the sharpest images, but may also introduce some ringing artifacts.
  • Bitmap.FILTER_BOX - Box filter (aka mean filter, averaging filter, and smoothing filter) produces blurry images and is considered to have poor visual results. Very fast computation.
  • Bitmap.FILTER_BILINEAR - Bilinear filter produces good results for image reduction and enlargement, but displays sharp transition lines. Very fast computation. This filter is popular in rendering of the previews.

各种过滤参数使用后图片结果:

 

 

 

 

2. Bitmap.scaleInto(Bitmap dst, int filterType, int iAspectRatioOption)

public void scaleInto(Bitmap dst,
                      int filterType,
                      int iAspectRatioOption)
Parameters:
dst - Destination bitmap.
filterType - Interpolation filter type. May be one of:
preserveAspectRatio - - preserve aspect ratio option. May be one of:
Throws:
NullPointerException - Thrown if 'dst' is null.
IllegalArgumentException - Thrown if the preserveAspectRatio parameter is invalid.
IllegalArgumentException - Thrown if the destination bitmap is read-only.
IllegalArgumentException - Thrown if illegal filter type is specified.

SCALE_TO_FIT ?With this option the aspect ratio is preserved. In case the source and the output bitmaps have different aspect ratios the scale is MIN((height_out/height_in), (width_out/width_in)). In other words, it will scale to match the smallest destination dimension (height or width). The source bitmap data is rendered in the center of the output bitmap. The source bitmap is fit to the dimensions of the destination bitmap and a part of destination bitmap remains unchanged.

下图是ScaleApplication的运行截图:

ScaleApplication 源代码如下:
package com.rim.samples.device.ui.bitmapscalingdemo;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;

/**
 *  A sample application to demonstrate Bitmap scaling
 */
public class BitmapScalingDemo extends UiApplication
{
    /**
     * Entry point for application
     * @param args Command line arguments (not used)
     */
    public static void main(String[] args)
    {
        // Create a new instance of the application and make the currently
        // running thread the application's event dispatch thread.
        BitmapScalingDemo app = new BitmapScalingDemo();
        app.enterEventDispatcher();       
    }

    /**
     * Creates a new BitmapScalingDemo object
     */
    public BitmapScalingDemo()
    {
        pushScreen(new BitmapScalingDemoScreen());
    }
   
    /**
     * MainScreen class for the BitmapScalingDemo application
     */
    static class BitmapScalingDemoScreen extends MainScreen
    {
        private static String LABEL_X = " x ";
       
        /**
         * Creates a new BitmapScalingDemoScreen object
         */
        BitmapScalingDemoScreen()
        {
            setTitle("Bitmap Scaling Demo");            
            
            // Create a Bitmap from a project resource
            Bitmap bitmapOrig = Bitmap.getBitmapResource("rim.png");           
           
            // Create a Bitmap of arbitrary size
            int scaledX = 175;
            int scaledY = 50;
            Bitmap bitmapScaled = new Bitmap(scaledX, scaledY);
           
            // Scale the original Bitmap into the new Bitmap using
            // a Lanczos filter.           
            bitmapOrig.scaleInto(bitmapScaled, Bitmap.FILTER_LANCZOS);                      
           
            // Display the original Bitmap on the screen
            BitmapField bitmapFieldOrig = new BitmapField(bitmapOrig, Field.FOCUSABLE);
            StringBuffer strBuff = new StringBuffer("Original - ");
            strBuff.append(bitmapOrig.getWidth());
            strBuff.append(LABEL_X);
            strBuff.append(bitmapOrig.getHeight());           
            add(new LabelField(strBuff.toString()));
            add(bitmapFieldOrig);
           
            add(new SeparatorField());
           
            // Display the scaled Bitmap on the screen
            BitmapField bitmapFieldScaled1 = new BitmapField(bitmapScaled, Field.FOCUSABLE);
            strBuff.delete(0, strBuff.length());
            strBuff.append("/nScaled - ");
            strBuff.append(bitmapScaled.getWidth());
            strBuff.append(LABEL_X);
            strBuff.append(bitmapScaled.getHeight());
            strBuff.append(" - FILTER_LANCZOS - Aspect ratio not preserved");
            add(new LabelField(strBuff.toString()));
            add(bitmapFieldScaled1);
           
            add(new SeparatorField());
           
            // Redefine the scaled Bitmap
            bitmapScaled = new Bitmap(scaledX, scaledY);           
           
            // Scale the original Bitmap into the new Bitmap using
            // a bilinear filter and maintaining aspect ratio.           
            bitmapOrig.scaleInto(bitmapScaled, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FILL);                      
           
            // Display the newly scaled Bitmap on the screen           
            BitmapField bitmapFieldScaled2 = new BitmapField(bitmapScaled, Field.FOCUSABLE);
            strBuff.delete(0, strBuff.length());
            strBuff.append("/nScaled - ");
            strBuff.append(bitmapScaled.getWidth());
            strBuff.append(LABEL_X);
            strBuff.append(bitmapScaled.getHeight());
            strBuff.append(" - FILTER_BILINEAR - Aspect ratio preserved");          
            add(new LabelField(strBuff.toString()));
            add(bitmapFieldScaled2);
           
            add(new SeparatorField());         
           
            // Redefine the scaled Bitmap
            bitmapScaled = new Bitmap(scaledX, scaledY);
           
            // Calculate fragment dimensions         
            int fragmentWidth = bitmapOrig.getWidth() >> 1; // >> 1 equivalent to / 2           
            int fragmentHeight = bitmapOrig.getHeight() >> 1; // >> 1 equivalent to / 2
           
            // Scale a fragment of the original Bitmap into the new Bitmap
            // using a box filter.
            bitmapOrig.scaleInto(0, 0, fragmentWidth, fragmentHeight, bitmapScaled, 0, 0, bitmapScaled.getWidth(), bitmapScaled.getHeight(), Bitmap.FILTER_BOX);
           
            // Display the newly scaled Bitmap on the screen           
            BitmapField bitmapFieldScaled3 = new BitmapField(bitmapScaled, Field.FOCUSABLE);           
            strBuff.delete(0, strBuff.length());
            strBuff.append("/nScaled fragment ");
            strBuff.append(fragmentWidth);
            strBuff.append(LABEL_X);
            strBuff.append(fragmentHeight);
            strBuff.append(" into ");
            strBuff.append(bitmapScaled.getWidth());
            strBuff.append(LABEL_X);
            strBuff.append(bitmapScaled.getHeight());
            strBuff.append(" - FILTER_BOX");                     
            add(new LabelField(strBuff.toString()));
            add(bitmapFieldScaled3);
        }       
    }
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值