I am currently doing android app for plant recognition using machine learning model which is deployed in Heroku. I capture image using camera implemented in my app and I want to send that image into api deployed in heroku, which will then predict the image and send me back the response in a string format which tells us that to which species it belongs to.
public class MainActivity extends AppCompatActivity {
private final int CAMERA_REQ_CODE=100;
private final int GALLERY_REQ_CODE=1000;
ImageView imgCam;
Bitmap img;
String url = "https://medkit.herokuapp.com/predict";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgCam=findViewById(R.id.imgCamera);
Button btnCamera=findViewById(R.id.btnCamera);
Button btnGallery=findViewById(R.id.btnGallery);
Button btnPredict=findViewById(R.id.btnPredict);
TextView result=findViewById(R.id.resultText);
btnCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent iCamera=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(iCamera,CAMERA_REQ_CODE);
}
});
btnGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent iGallery=new Intent(Intent.ACTION_PICK);
iGallery.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(iGallery,GALLERY_REQ_CODE);
}
});
btnPredict.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
String data = jsonObject.getString("y_pred_mobile");
if (data.equals("1")) {
result.setText("Hibiscus");
} else if (data.equals("2")) {
result.setText("Aloevera");
} else if (data.equals("3")) {
result.setText("Brahmi");
} else if (data.equals("4")) {
result.setText("Neem");
} else {
result.setText("not in the selected species");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
}) {@override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String,String>();
params.put("cgpa",cgpa.getText().toString());
params.put("iq",iq.getText().toString());
params.put("profile_score",profile_score.getText().toString());
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
queue.add(stringRequest);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK)
{
if(requestCode==CAMERA_REQ_CODE)
{
//for camera
Bitmap img=(Bitmap)(data.getExtras().get("data"));
imgCam.setImageBitmap(img);
}
if(requestCode==GALLERY_REQ_CODE)
{
//for Gallery
imgCam.setImageURI(data.getData());
}
}
}
} ` above is my app code. Rest all works fine but i need a code that will put image instead of string for example
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String,String>();
params.put("cgpa",cgpa.getText().toString());
params.put("iq",iq.getText().toString());
params.put("profile_score",profile_score.getText().toString());
return params;
this code is to send string as a data , I need a code that can send image as data to the api instead of string like in the above code
Can anyone please help me to fix this ?