Advertisement
Guest User

Scan_IDAsync

a guest
Oct 18th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.06 KB | None | 0 0
  1. package com.juaracoding.dukcapil_test_api;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.app.Activity;
  6. import android.content.Intent;
  7. import android.content.SharedPreferences;
  8. import android.graphics.Bitmap;
  9. import android.graphics.BitmapFactory;
  10. import android.net.Uri;
  11. import android.os.Bundle;
  12. import android.preference.PreferenceManager;
  13. import android.util.Base64;
  14. import android.util.Log;
  15. import android.view.View;
  16. import android.widget.Button;
  17. import android.widget.ImageView;
  18. import android.widget.ProgressBar;
  19. import android.widget.TextView;
  20. import android.widget.Toast;
  21.  
  22. import com.juaracoding.dukcapil_test_api.ApiService.ApiClient;
  23. import com.juaracoding.dukcapil_test_api.ApiService.ApiClientBearerToken;
  24. import com.juaracoding.dukcapil_test_api.ApiService.ApiInterfaceREST;
  25. import com.juaracoding.dukcapil_test_api.ApiService.AppUtil;
  26. import com.juaracoding.dukcapil_test_api.Model.Status;
  27.  
  28. import org.json.JSONException;
  29. import org.json.JSONObject;
  30.  
  31. import java.io.BufferedReader;
  32. import java.io.ByteArrayOutputStream;
  33. import java.io.File;
  34. import java.io.FileInputStream;
  35. import java.io.FileNotFoundException;
  36. import java.io.IOException;
  37. import java.io.InputStream;
  38. import java.io.InputStreamReader;
  39.  
  40. import okhttp3.MediaType;
  41. import okhttp3.RequestBody;
  42. import retrofit2.Call;
  43. import retrofit2.Callback;
  44. import retrofit2.Response;
  45.  
  46. public class Scan_IDAsync extends AppCompatActivity {
  47.  
  48. private static final int GALLERY_REQUEST_CODE = 1;
  49. Uri selectedImage;
  50. Bitmap bitmap;
  51. InputStream imageStream;
  52. ImageView imgKtp, faceImg, signImg;
  53. Button browseGallery, identifying, nextPage;
  54. ProgressBar progressBar;
  55. TextView nik;
  56. @Override
  57. protected void onCreate(Bundle savedInstanceState) {
  58. super.onCreate(savedInstanceState);
  59. setContentView(R.layout.activity_scan__idasync);
  60. imgKtp = findViewById(R.id.imgKtp);
  61. faceImg = findViewById(R.id.faceImg);
  62. signImg = findViewById(R.id.signImg);
  63. browseGallery = findViewById(R.id.browseGallery);
  64. identifying = findViewById(R.id.identifying);
  65. nextPage = findViewById(R.id.nextPage);
  66. nik = findViewById(R.id.nik);
  67. progressBar= findViewById(R.id.progressBar);
  68. progressBar.setVisibility(View.GONE);
  69. nextPage.setActivated(false);
  70.  
  71. browseGallery.setOnClickListener(new View.OnClickListener() {
  72. @Override
  73. public void onClick(View view) {
  74. pickFromGallery();
  75.  
  76. }
  77. });
  78.  
  79. identifying.setOnClickListener(new View.OnClickListener() {
  80. @Override
  81. public void onClick(View view) {
  82. getData();
  83. }
  84. });
  85.  
  86. if(faceImg != null && signImg != null){
  87. nextPage.setActivated(true);
  88. nextPage.setOnClickListener(new View.OnClickListener() {
  89. @Override
  90. public void onClick(View view) {
  91. Intent intent = new Intent(Scan_IDAsync.this, Get_Dukcapil_InfoAsync.class);
  92. SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(Scan_IDAsync.this);
  93. prefs.edit().putString("nik", nik.getText().toString()).commit();
  94. startActivity(intent);
  95. }
  96. });
  97. }
  98.  
  99. }
  100.  
  101. private void pickFromGallery(){
  102. //Create an Intent with action as ACTION_PICK
  103. Intent intent=new Intent(Intent.ACTION_PICK);
  104. // Sets the type as image/*. This ensures only components of type image are selected
  105. intent.setType("image/*");
  106. startActivityForResult(intent,GALLERY_REQUEST_CODE);
  107. }
  108.  
  109. public void onActivityResult(int requestCode,int resultCode,Intent data){
  110. // Result code is RESULT_OK only if the user selects an Image
  111. if (resultCode == Activity.RESULT_OK && requestCode == GALLERY_REQUEST_CODE){
  112. //data.getData returns the content URI for the selected Image
  113. try {
  114. selectedImage = data.getData();
  115. imageStream = getContentResolver().openInputStream(selectedImage);
  116. bitmap = BitmapFactory.decodeStream(imageStream);
  117. imgKtp.setImageBitmap(bitmap);
  118. encodeImage(bitmap);
  119.  
  120. } catch (FileNotFoundException e) {
  121. e.printStackTrace();
  122. Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG).show();
  123. }
  124. }
  125.  
  126. }
  127. private String encodeImage(Bitmap bm)
  128. {
  129. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  130. bm.compress(Bitmap.CompressFormat.JPEG,100,baos);
  131. byte[] b = baos.toByteArray();
  132. String encImage = Base64.encodeToString(b, Base64.DEFAULT);
  133. return encImage;
  134. }
  135.  
  136.  
  137. ApiInterfaceREST apiInterface;
  138. Status status;
  139. private void getData(){
  140. apiInterface = ApiClientBearerToken.getClientWithApi().create(ApiInterfaceREST.class);
  141. JSONObject rBody = new JSONObject();
  142. try {
  143. rBody.put("default_lang", AppUtil.DEFAULT_LANG);
  144. rBody.put("base64_id", "data:image/jpeg;base64," + encodeImage(bitmap));
  145.  
  146. }catch (JSONException e){
  147.  
  148. }
  149. final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
  150. String token = prefs.getString("Token", "Token");
  151. Call<Status> statusCall = apiInterface.getData("Bearer " + token, RequestBody.create(MediaType.parse("application/json"), rBody.toString()));
  152. progressBar.setVisibility(View.VISIBLE);
  153. statusCall.enqueue(new Callback<Status>() {
  154. @Override
  155. public void onResponse(Call<Status> call, Response<Status> response) {
  156. if(response.body() != null){
  157. progressBar.setVisibility(View.GONE);
  158. Toast.makeText(getApplicationContext(), "Status Confirmed", Toast.LENGTH_LONG).show();
  159.  
  160.  
  161. status = response.body();
  162.  
  163. nik.setText(status.getPayload().getData().getNik());
  164.  
  165.  
  166.  
  167. String Face = status.getPayload().getData().getBase64Photo();
  168. String Sign = status.getPayload().getData().getBase64Signature();
  169. Face.replace("data:image/jpeg;base64,", "");
  170. Sign.replace("data:image/jpeg;base64,", "");
  171. String Face2 = Face.split(",")[1];
  172. String Sign2 = Sign.split(",")[1];
  173.  
  174. byte[] decodedStringFace = Base64.decode(Face2, Base64.DEFAULT);
  175. Bitmap decodedByteFace = BitmapFactory.decodeByteArray(decodedStringFace, 0, decodedStringFace.length);
  176.  
  177. faceImg.setImageBitmap(decodedByteFace);
  178.  
  179. byte[] decodedStringSign = Base64.decode(Sign2, Base64.DEFAULT);
  180. Bitmap decodedByteSign = BitmapFactory.decodeByteArray(decodedStringSign, 0, decodedStringSign.length);
  181.  
  182. signImg.setImageBitmap(decodedByteSign);
  183. }else {
  184. try {
  185. JSONObject jObjError = new JSONObject(response.errorBody().string());
  186. Toast.makeText(Scan_IDAsync.this, jObjError.getString("message"), Toast.LENGTH_LONG).show();
  187. } catch (Exception e) {
  188. Toast.makeText(Scan_IDAsync.this, e.getMessage(), Toast.LENGTH_LONG).show();
  189. Log.e("msg", e.getMessage());
  190. }
  191. }
  192. }
  193.  
  194. @Override
  195. public void onFailure(Call<Status> call, Throwable t) {
  196. Log.e("msg", t.getMessage());
  197. Toast.makeText(getApplicationContext(), "There is connection problem!!!", Toast.LENGTH_LONG).show();
  198. call.cancel();
  199. }
  200. });
  201. }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement