-1

サーバーに送信する必要がある画像があります。ブラックベリーでビットマップ(jpg)をbase64文字列に変換する方法はありますか?

4

3 に答える 3

6

あなたの要求は少し曖昧で奇妙ですが、これが役に立てば幸いです:

次のピース コードを使用すると、JPEG バイナリ データを取得できますBitmap(これは圧縮されているため、生のビットマップと比較すると、データのサイズは可能な限り小さくなっています)。

Bitmap bmp = ...; // your bitmap
int quality = 85;
EncodedImage encodedImg = JPEGEncodedImage.encode(bmp, quality);
byte[] data = encodedImg.getData();

次に、でエンコードできますBase64OutputStreamエンコード方法のサンプル コードについては、API を参照してください。

于 2013-01-03T21:06:18.187 に答える
1
package com.covidien.screens;

import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.CDMAInfo;
import net.rim.device.api.system.GPRSInfo;
import net.rim.device.api.system.IDENInfo;
import net.rim.device.api.system.RadioInfo;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;

import org.kobjects.base64.Base64;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransport;


public final class ImageScreen extends MainScreen
{
    /** The down-scaling ratio applied to the snapshot Bitmap */
    private static final int IMAGE_SCALING = 5;
    private static final String boundary = "31BF3856AD364E35";
    /** The base file name used to store pictures */
    private static String FILE_NAME = System.getProperty("fileconn.dir.photos") + "IMAGE";

    /** The extension of the pictures to be saved */
    private static String EXTENSION = ".png";

    /** A counter for the number of snapshots taken */
    private static int _counter;

    /** A reference to the current screen for listeners */
    private ImageScreen _imageScreen;

    static String imageName=null;
    /**
    * Constructor
    * @param raw A byte array representing an image
    */
    public ImageScreen( final byte[] raw )
    {
        // A reference to this object, to be used in listeners
        _imageScreen = this;

        setTitle("IMAGE");

        // Convert the byte array to a Bitmap image
        Bitmap image = Bitmap.createBitmapFromBytes( raw, 0, -1, IMAGE_SCALING );

        // Create two field managers to center the screen's contents
        HorizontalFieldManager hfm1 = new HorizontalFieldManager( Field.FIELD_HCENTER );
        HorizontalFieldManager hfm2 = new HorizontalFieldManager( Field.FIELD_HCENTER );

        // Create the field that contains the image
        BitmapField imageField = new BitmapField( image );
        hfm1.add( imageField );

        // Create the SAVE button which returns the user to the main camera
        // screen and saves the picture as a file.
        ButtonField photoButton = new ButtonField( "Use" );
        photoButton.setChangeListener( new SaveListener(raw) );
        hfm2.add(photoButton);

        // Create the CANCEL button which returns the user to the main camera
        // screen without saving the picture.
        ButtonField cancelButton = new ButtonField( "Retake" );
        cancelButton.setChangeListener( new CancelListener() );
        hfm2.add(cancelButton);

        // Add the field managers to the screen
        add( hfm1 );
        add( hfm2 );
    }

    /**
    * Handles trackball click events
    * @see net.rim.device.api.ui.Screen#invokeAction(int)
    */
    protected boolean invokeAction(int action)
    {
        boolean handled = super.invokeAction(action);

        if(!handled)
        {
            switch(action)
            {
                case ACTION_INVOKE: // Trackball click.
                {
                    return true;
                }
            }
        }
        return handled;
    }

    /**
    * A listener used for the "Save" button
    */
    private class SaveListener implements FieldChangeListener
    {


        /** A byte array representing an image */
        private byte[] _raw;

        /**
        * Constructor.
        * @param raw A byte array representing an image
        */
        SaveListener(byte[] raw)
        {
            _raw = raw;
        }

        /**
        * Saves the image as a file in the BlackBerry filesystem
        */
        public void fieldChanged(Field field, int context)
        {
            try
            {
                // Create the connection to a file that may or
                // may not exist.
                FileConnection file = (FileConnection)Connector.open( FILE_NAME + _counter + EXTENSION );

                // If the file exists, increment the counter until we find
                // one that hasn't been created yet.
                while( file.exists() )
                {
                    file.close();
                    ++_counter;
                    file = (FileConnection)Connector.open( FILE_NAME + _counter + EXTENSION );
                }

                // We know the file doesn't exist yet, so create it
                file.create();

                // Write the image to the file
                OutputStream out = file.openOutputStream();
                out.write(_raw);

                System.out.println("Boundary  :::::"+boundary);
                //*******************************************************************************************************
                String serviceUrl = "URL/Service.asmx";
                String serviceNamespace = "http://tempuri.org/";
                String soapAction="http://tempuri.org/Upload";
                SoapObject rpc = new SoapObject(serviceNamespace, "Upload");
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

                envelope.bodyOut = rpc;
                envelope.dotNet = true;
                envelope.encodingStyle = SoapSerializationEnvelope.XSD;
                rpc.addProperty("contents",Base64.encode(_raw));
                imageName="Image" + _counter + EXTENSION;
                rpc.addProperty("FileName", imageName );
                HttpTransport ht = new HttpTransport(serviceUrl);
                ht.debug = true;
                String result;

                //                String str = null;

                SoapObject soapObject;

                try
                {
                    ht.call(soapAction, envelope);
                    result = (envelope.getResponse()).toString();
                    //                      if((envelope.getResponse()).toString().trim().equals("OK"))
                    //                      {
                        //                         UiApplication.getUiApplication().pushScreen(new DoctorPopup());
                    //                     }
                    //                    if(result.length()==2 ||  result.equalsIgnoreCase("OK"))
                    //                     {
                        //                       UiApplication.getUiApplication().pushScreen(new DoctorPopup());
                    //                     }

                    if(result.length()==2)
                    {
                        UiApplication.getUiApplication().pushScreen(new DoctorPopup());
                    }

                    soapObject = (SoapObject) envelope.getResponse();
                    //                     Dialog.alert("soapObject" + soapObject);
                }
                catch(Exception ex)
                {
                    //if we get an exception we'll just write the msg to the screen.
                    System.out.println(ex.getMessage());
                    result = ex.toString();
                }



                // Close the connections
                out.close();
                file.close();
            }
            catch(Exception e)
            {
                WelcomeScreen.errorDialog("ERROR " + e.getClass() + ":  " +    e.getMessage());
                System.out.println(e.getMessage());
            }

            ++_counter;
        }   
    }

    /**
    * A listener used for the "Cancel" button
    */
    private class CancelListener implements FieldChangeListener
    {
        /**
        * Return to the main camera screen
        */
        public void fieldChanged(Field field, int context)
        {
            UiApplication.getUiApplication().popScreen( _imageScreen );
        }
    }

    public final static boolean isCDMA() {
        return RadioInfo.getNetworkType() == RadioInfo.NETWORK_CDMA;
    }

    public final static boolean isIDEN() {
        return RadioInfo.getNetworkType() == RadioInfo.NETWORK_IDEN;
    }

    public static final String getIMEI() {
        if (ImageScreen.isCDMA()) {
            return ""+CDMAInfo.getESN();
        } else if (ImageScreen.isIDEN()){
            return IDENInfo.imeiToString(IDENInfo.getIMEI());
        } else {
            return GPRSInfo.imeiToString(GPRSInfo.getIMEI());
        }
    }
}
于 2013-01-02T06:29:40.313 に答える