Convert bitmap to base64 string in blackberry

后端 未结 3 1267
没有蜡笔的小新
没有蜡笔的小新 2021-01-29 08:25

I have an image which needs to be sent to the server. Is there a way to convert a bitmap(jpg) to base64 string in blackberry?

3条回答
  •  遇见更好的自我
    2021-01-29 09:02

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

提交回复
热议问题