Dynamic conversion of images to binary and vice versa

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

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

Which mechanism is best suited for this.

+4
source share
4 answers

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

  • create a bitmap variable before you do anything.
  • to set the image name to the SD card.
  • this variable, you must put the path to the file, this is for you.
  • sendData is the name of the function to call it, you can use something like sendData(null) .
  • Remember to wrap it in 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"); } } 
+6
source

The image is in Bitmap, and then use the following code to convert this image to binary. Using the 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 an image from binary, use the following example:

 Bitmap bMap = null; bMap = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length); 
+8
source

Try this. Let img contain a bitmap.

 ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(img, "png", baos); baos.flush(); byte[] imageInByte = baos.toByteArray(); baos.close(); 

imageInByte now contains a bytedata bitmap.

To convert the inverse

 Bitmap bp = BitmapFactory.decodeByteArray(imgArray, 0,imgArray.length); 

Hope this helps you.

+2
source

if you want to send an HttpPost request to the web server using HttpClient

+1
source

Source: https://habr.com/ru/post/1390827/


All Articles