0

我想裁剪从服务器获取的图像。我尝试了以下代码 -

URLBitmapField wmf= new URLBitmapField("http://a0.twimg.com//sticky//default_profile_images//default_profile_4_normal.png");

URLBitmapField 类如下 -

import net.rim.device.api.math.Fixed32;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;

//local

public class URLBitmapField extends BitmapField implements URLDataCallback {
EncodedImage result = null;
Bitmap myImage;

public static EncodedImage _encoded_img = null;

int _imgWidth = 52;
int _imgHeight = 62;
int _imgMargin = 10;

public URLBitmapField(String url) {
    try {
        http_image_data_extrator.getWebData(url, this);
    }
    catch (Exception e) {}
}

public Bitmap getBitmap() {
    if (_encoded_img == null) return null;
    return myImage;
}

public void callback(final String data) {
    if (data.startsWith("Exception")) return;

    try {
        byte[] dataArray = data.getBytes();

        //bitmap = EncodedImage.createEncodedImage(dataArray, 0, dataArray.length);//no scale

        _encoded_img = EncodedImage.createEncodedImage(dataArray, 0, dataArray.length); // with scale
        _encoded_img = sizeImage(_encoded_img, _imgWidth, _imgHeight);

        setImage(_encoded_img);
        UiApplication.getUiApplication().getActiveScreen().invalidate();
    }
    catch (final Exception e){}
}

public EncodedImage sizeImage(EncodedImage image, int width, int height) {


      int currentWidthFixed32 = Fixed32.toFP(image.getWidth());
      int currentHeightFixed32 = Fixed32.toFP(image.getHeight());

      int requiredWidthFixed32 = Fixed32.toFP(width);
      int requiredHeightFixed32 = Fixed32.toFP(height);

      int scaleXFixed32 = Fixed32.div(currentWidthFixed32,
        requiredWidthFixed32);
      int scaleYFixed32 = Fixed32.div(currentHeightFixed32,
        requiredHeightFixed32);

      result = image.scaleImage32(scaleXFixed32, scaleYFixed32);
      return result;
}

}





public interface URLDataCallback {

    public void callback(String data);

}



import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import net.rim.device.api.system.RadioInfo;
import net.rim.device.api.system.WLANInfo;
import net.rim.device.api.ui.UiApplication;


public class http_image_data_extrator {
static String url_="";
static StringBuffer rawResponse=null;
//static String result = null;
     public static void getWebData(String url, final URLDataCallback callback) throws IOException {
         //url_=url;

                 HttpConnection connection = null;  
                 InputStream inputStream = null;  

                try {  


                    if ((WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)
                            && RadioInfo
                                    .areWAFsSupported(RadioInfo.WAF_WLAN)) {
                        url += ";interface=wifi";
                    }

                    connection = (HttpConnection) Connector.open(url, Connector.READ, true);  

                    String location=connection.getHeaderField("location");

                    if(location!=null){


                        if ((WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)
                                && RadioInfo
                                        .areWAFsSupported(RadioInfo.WAF_WLAN)) {
                            location += ";interface=wifi";
                        }


                        connection = (HttpConnection) Connector.open(location, Connector.READ, true);  

                    }else{
                        connection = (HttpConnection) Connector.open(url, Connector.READ, true);  
                    }


                    inputStream = connection.openInputStream();  
                    byte[] responseData = new byte[10000];  
                    int length = 0;  
                    rawResponse = new StringBuffer();  
                    while (-1 != (length = inputStream.read(responseData))) { 
                        rawResponse.append(new String(responseData, 0, length));  
                    }  
                    int responseCode = connection.getResponseCode();  
                    if (responseCode != HttpConnection.HTTP_OK){
                        throw new IOException("HTTP response code: "+ responseCode);  
                    }  

                    final String  result = rawResponse.toString();
                    UiApplication.getUiApplication().invokeLater(new Runnable() {  
                        public void run(){  
                            callback.callback(result);  
                        }  
                    });  
                }  
                catch (final Exception ex) {  
                    UiApplication.getUiApplication().invokeLater(new Runnable() {  
                        public void run() {  
                            callback.callback("Exception (" + ex.getClass() + "): " + ex.getMessage());  
                        }  
                    });  
                }
    }  

}

我也有图像裁剪的代码 -

    int width = myImage.getWidth();
    int height = myImage.getHeight();

    // get original data from the image
    int myImageData[] = new int[width * height];
    myImage.getARGB(myImageData, 0, width, 0, 0, width, height);

    // get default color of a newly created bitmap
    int defaultColors[] = new int[1 * 1];
    (new Bitmap(1, 1)).getARGB(defaultColors, 0, 1, 0, 0, 1, 1);

    int defaultColor = defaultColors[0];
    int fillColor = Color.RED;
    int diameter = 65;

    // dummy data preparation
    Bitmap dummyImage = new Bitmap(width, height);
    Graphics dummyImageGraphics = Graphics.create(dummyImage);
    dummyImageGraphics.setColor(fillColor);
    int startX = width / 2 - diameter / 2;
    int startY = height / 2 - diameter / 2;
    dummyImageGraphics.fillArc(startX, startY, diameter, diameter, 0, 360);
    int dummyData[] = new int[width * height];
    dummyImage.getARGB(dummyData, 0, width, 0, 0, width, height);

    // filling original data with transparent value.
    int totalPixels = width * height;
    for (int i = 0; i < totalPixels; i++) {
        if (dummyData[i] == defaultColor) {
            myImageData[i] = 0;
        }
    }

    // set new data
    myImage.setARGB(myImageData, 0, width, 0, 0, width, height);

如何从 url 裁剪图像?在哪里添加裁剪代码?

4

1 回答 1

0

简单的:

    public static Bitmap cropBitmap(Bitmap src, int w, int h) {
        Bitmap cropped = new Bitmap(w, h);
        Graphics g = new Graphics(cropped);
        g.drawBitmap(0, 0, w, h, src, 0, 0);
        return cropped;
    }

至于缩放,看看Bitmap.scaleInto方法。

于 2012-05-25T10:51:11.703 回答