Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically conversion of image to binary and vice versa

Tags:

android

image

How can I convert image to binary data..???

I want to send that converted binary data to another device or to the web server.

Which mechanism is best to do this.?

like image 632
SilentKiller Avatar asked Jan 13 '12 12:01

SilentKiller


People also ask

Why do we convert images to binary?

The main reason binary images are particularly useful in the field of Image Processing is because they allow easy separation of an object from the background. The process of segmentation allows to label each pixel as 'background' or 'object' and assigns corresponding black and white colours.

How do you convert an image to binary in Matlab?

BW = im2bw( I , level ) converts the grayscale image I to binary image BW , by replacing all pixels in the input image with luminance greater than level with the value 1 (white) and replacing all other pixels with the value 0 (black).


2 Answers

Image is in Bitmap then use the following code to convert that image to binary. By using following code

Bitmap photo;// this is your image.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

To get Image From Binary use the following sample:

Bitmap bMap = null;

bMap = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
like image 107
Sumant Avatar answered Nov 03 '22 00:11

Sumant


I found a good example for uploading the image to the server.

  • create a bitmap variable before do anything.
  • variable to set a name to the image into SD card.
  • this variable, you have to put the path for the File, It's up to you.
  • sendData is the function name, to call it, you can use something like sendData(null).
  • remember to wrap it into a try catch.

private Bitmap bitmap;
public static String exsistingFileName = "";

public void sendData(String[] args) throws Exception {
    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();

        // here, change it to your php;
        HttpPost httpPost = new HttpPost("http://www.myURL.com/myPHP.php");
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        bitmap = BitmapFactory.decodeFile(exsistingFileName);

        // you can change the format of you image compressed for what do you want;
        // now it is set up to 640 x 480;
        Bitmap bmpCompressed = Bitmap.createScaledBitmap(bitmap, 640, 480, true);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        // CompressFormat set up to JPG, you can change to PNG or whatever you want;
        bmpCompressed.compress(CompressFormat.JPEG, 100, bos);
        byte[] data = bos.toByteArray();

        // sending a String param;
        entity.addPart("myParam", new StringBody("my value"));

        // sending a Image;
        // note here, that you can send more than one image, just add another param, same rule to the String;
        entity.addPart("myImage", new ByteArrayBody(data, "temp.jpg"));
        httpPost.setEntity(entity);
        HttpResponse response = httpClient.execute(httpPost, localContext);
        BufferedReader reader = new BufferedReader(new InputStreamReader(   response.getEntity().getContent(), "UTF-8"));
        String sResponse = reader.readLine();

    } catch (Exception e) {
        Log.v("myApp", "Some error came up");
    }
}
like image 28
SilentKiller Avatar answered Nov 03 '22 01:11

SilentKiller