2

Here is the code that I wrote. When I use FormBody.Builder to add the picture's Base64 string, it takes hundreds of milliseconds.

I have no idea to solve this problem.

ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap cropBmp = Bitmap.createBitmap(argb, region.getOffset(), 
region.getBmpWidth(), region.getRegion().width(), region.getRegion().height(), Bitmap.Config.ARGB_8888);                                  
cropBmp.compress(Bitmap.CompressFormat.JPEG, 100, baos); 

String picStr = Base64.encodeToString(baos.toByteArray(), Base64.NO_WRAP);
baos.reset();        

if (picStr != null && picStr.length() / SIZE_M_UNIT <= 10&&!TextUtils.isEmpty(access_token)) {                                      
    String url = BaiduFaceRequest.searchUrl + accessParam + access_token;                                     
    FormBody.Builder fb = new FormBody.Builder();
    fb.add("image", picStr);
    fb.add("group_id", BaiduFaceConfig.groupID);
    Request.Builder rb = new Request.Builder();
    rb.url(url).post(fb.build());
}

How can I optimize the code?

rcgeorge23
  • 3,594
  • 4
  • 29
  • 54
W.Marshall
  • 21
  • 2
  • 1
    hundreds of millisecond is not even a second right? why should concern, iI'm uploading 9 image about 3 second with volley – Iqbal Rizky Mar 03 '18 at 09:37
  • What hardware/mobile device are you running on? What sort of times do you expect? What size are the images? Can you provide a sample representative image? In comparison what operations on the images are fast, e.g. does writing to disk only take 10 ms? – Yuri Schimke Mar 03 '18 at 12:22
  • The size of the image base64 string is smaller than 1M.I scaned the source code of the FormBody class,and I found the 'add' method that used the for-loop structure. – W.Marshall Mar 04 '18 at 02:48

0 Answers0