0

i am trying to build an android app that takes an image from the camera. Then i send that image to a web service that i build and edit it. At this moment i am just trying to send the image and just get it back and show it in an imageView. I am just trying to see how is going to work with the web service. I am sending byte[] and receive byte[].Do i have to make a convert to the web service and return a different type or it ok to return byte[]? i use ksoap2 to connect with the web service. i don't know what to do when i get the result and how to convert the result to bitmap! Any Help???????

Code:

    if (requestCode == CAMERA_DATA)
        if (resultCode == Activity.RESULT_OK) {
            Bundle extras = data.getExtras();
            bitmap = (Bitmap) extras.get("data");

            setContentView(R.layout.photo3);
            image = (ImageView) findViewById (R.id.imageView);

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            bitmap.compress(CompressFormat.JPEG, 100, out);
            data1 = out.toByteArray();

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
            request.addProperty("name",data1);  


            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
            envelope.dotNet = false;
            envelope.setOutputSoapObject(request);

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

              try {
               androidHttpTransport.call(SOAP_ACTION, envelope);

                SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();

                //WHAT TO DO HERE

                image.setImageBitmap(btm);



              } catch (Exception e) {


              }






            try {
                out.close();
            }
            catch(IOException e){

            }
     }
Katerina
  • 187
  • 3
  • 16

2 Answers2

1

Hello Please check code below

The first method is used to convert file in =to base64 and second method is for compressing any image. you can use these code to encode into base64 and add soap parameter string which is returned from first method

private String getEncodeData(String filePath) {
            String encodedimage1 = null;
            if (filePath != null && filePath.length() > 0) {
                try {
                    Bitmap bm = decodeFile(new File (filePath));
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
                    bm.compress(Bitmap.CompressFormat.PNG, 50, baos); //bm is the bitmap object 
                    byte[] b = baos.toByteArray();
                    encodedimage1= Base64.encodeToString(b, Base64.DEFAULT);
                } catch (Exception e) {
                    System.out
                    .println("Exception: In getEncodeData" + e.toString());
                }
            }
            return encodedimage1;
        }

private Bitmap decodeFile(File f){
        Bitmap b = null;
        final int IMAGE_MAX_SIZE = 100;
        try {
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            FileInputStream fis = new FileInputStream(f);
            BitmapFactory.decodeStream(fis, null, o);
            fis.close();
            int scale = 1;
            if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
                scale = (int) Math.pow(2.0, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
            }
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            fis = new FileInputStream(f);
            b = BitmapFactory.decodeStream(fis, null, o2);
            fis.close();
        } catch (Exception e) {
            Log.v("Exception in decodeFile() ",e.toString()+"");
        }
        return b;
    }

Please let me know about any more difficulties

Abhinav Singh Maurya
  • 3,313
  • 8
  • 33
  • 51
  • returns me an error in line: encodedimage1= Base64.encodeToString(b, Base64.DEFAULT); – Katerina Dec 06 '11 at 23:24
  • @ Abhinav Singh Maurya in getEncodeData(String filePath) how can i get the filePath of the photo and give it to that method...i am using a real device to run my app.. – Katerina Dec 06 '11 at 23:27
  • You can get file path from where it is stored using Environment.getExternalStorageDirectory(); this will give you path to your SD card and where your file is save give that path to itEX: Environment.getExternalStorageDirectory()+"/MyFolder/Imagename.jpg" – Abhinav Singh Maurya Dec 07 '11 at 13:41
0

What does the byte array represent? Is this a file in some specific format? (PNG, JPEG, etc.) or is it raw bytes representing the RGB(A) values of the image? In the last case the BufferedImage of the standard Java libs might be helpful. Especially the setRGB method: http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/image/BufferedImage.html#setRGB(int,%20int,%20int,%20int,%20int[],%20int,%20int)

Simply instantiate a new BufferedImage of the desired dimensions and the correct image type, and use the setRGB method to paste in the pixel values. This can later be used to be displayed in an ImageView or simply saved on the device.

I hope this helps ...

kraenhansen
  • 1,535
  • 2
  • 15
  • 26
  • The BitmapFactory of Maneesh's post is also a good starting point: https://developer.android.com/reference/android/graphics/BitmapFactory.html – kraenhansen Dec 06 '11 at 11:33