您的位置:首页 > 移动开发

BlackBerry平台图片缩放功能-Bitmap

2010-08-08 12:25 218 查看
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:
Bitmap.FILTER_LANCZOS
Bitmap.FILTER_BOX
Bitmap.FILTER_BILINEAR
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:
Bitmap.FILTER_LANCZOS
Bitmap.FILTER_BOX
Bitmap.FILTER_BILINEAR
preserveAspectRatio
- - preserve aspect ratio option. May be one of:
Bitmap.SCALE_STRETCH
- aspect ratio is not preserved
Bitmap.SCALE_TO_FIT
?preserve aspect ratio and fit the source bitmap to the destination bitmap
Bitmap.SCALE_TO_FILL
?preserve aspect ratio and fill the destination bitmap with the source bitmapThrows:
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);        }           }}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐