Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.juaracoding.dukcapil_test_api;
- import androidx.appcompat.app.AppCompatActivity;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.net.Uri;
- import android.os.Bundle;
- import android.preference.PreferenceManager;
- import android.util.Base64;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.ProgressBar;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.juaracoding.dukcapil_test_api.ApiService.ApiClient;
- import com.juaracoding.dukcapil_test_api.ApiService.ApiClientBearerToken;
- import com.juaracoding.dukcapil_test_api.ApiService.ApiInterfaceREST;
- import com.juaracoding.dukcapil_test_api.ApiService.AppUtil;
- import com.juaracoding.dukcapil_test_api.Model.Status;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import okhttp3.MediaType;
- import okhttp3.RequestBody;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- public class Scan_IDAsync extends AppCompatActivity {
- private static final int GALLERY_REQUEST_CODE = 1;
- Uri selectedImage;
- Bitmap bitmap;
- InputStream imageStream;
- ImageView imgKtp, faceImg, signImg;
- Button browseGallery, identifying, nextPage;
- ProgressBar progressBar;
- TextView nik;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_scan__idasync);
- imgKtp = findViewById(R.id.imgKtp);
- faceImg = findViewById(R.id.faceImg);
- signImg = findViewById(R.id.signImg);
- browseGallery = findViewById(R.id.browseGallery);
- identifying = findViewById(R.id.identifying);
- nextPage = findViewById(R.id.nextPage);
- nik = findViewById(R.id.nik);
- progressBar= findViewById(R.id.progressBar);
- progressBar.setVisibility(View.GONE);
- nextPage.setActivated(false);
- browseGallery.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- pickFromGallery();
- }
- });
- identifying.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- getData();
- }
- });
- if(faceImg != null && signImg != null){
- nextPage.setActivated(true);
- nextPage.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent intent = new Intent(Scan_IDAsync.this, Get_Dukcapil_InfoAsync.class);
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(Scan_IDAsync.this);
- prefs.edit().putString("nik", nik.getText().toString()).commit();
- startActivity(intent);
- }
- });
- }
- }
- private void pickFromGallery(){
- //Create an Intent with action as ACTION_PICK
- Intent intent=new Intent(Intent.ACTION_PICK);
- // Sets the type as image/*. This ensures only components of type image are selected
- intent.setType("image/*");
- startActivityForResult(intent,GALLERY_REQUEST_CODE);
- }
- public void onActivityResult(int requestCode,int resultCode,Intent data){
- // Result code is RESULT_OK only if the user selects an Image
- if (resultCode == Activity.RESULT_OK && requestCode == GALLERY_REQUEST_CODE){
- //data.getData returns the content URI for the selected Image
- try {
- selectedImage = data.getData();
- imageStream = getContentResolver().openInputStream(selectedImage);
- bitmap = BitmapFactory.decodeStream(imageStream);
- imgKtp.setImageBitmap(bitmap);
- encodeImage(bitmap);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG).show();
- }
- }
- }
- private String encodeImage(Bitmap bm)
- {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- bm.compress(Bitmap.CompressFormat.JPEG,100,baos);
- byte[] b = baos.toByteArray();
- String encImage = Base64.encodeToString(b, Base64.DEFAULT);
- return encImage;
- }
- ApiInterfaceREST apiInterface;
- Status status;
- private void getData(){
- apiInterface = ApiClientBearerToken.getClientWithApi().create(ApiInterfaceREST.class);
- JSONObject rBody = new JSONObject();
- try {
- rBody.put("default_lang", AppUtil.DEFAULT_LANG);
- rBody.put("base64_id", "data:image/jpeg;base64," + encodeImage(bitmap));
- }catch (JSONException e){
- }
- final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
- String token = prefs.getString("Token", "Token");
- Call<Status> statusCall = apiInterface.getData("Bearer " + token, RequestBody.create(MediaType.parse("application/json"), rBody.toString()));
- progressBar.setVisibility(View.VISIBLE);
- statusCall.enqueue(new Callback<Status>() {
- @Override
- public void onResponse(Call<Status> call, Response<Status> response) {
- if(response.body() != null){
- progressBar.setVisibility(View.GONE);
- Toast.makeText(getApplicationContext(), "Status Confirmed", Toast.LENGTH_LONG).show();
- status = response.body();
- nik.setText(status.getPayload().getData().getNik());
- String Face = status.getPayload().getData().getBase64Photo();
- String Sign = status.getPayload().getData().getBase64Signature();
- Face.replace("data:image/jpeg;base64,", "");
- Sign.replace("data:image/jpeg;base64,", "");
- String Face2 = Face.split(",")[1];
- String Sign2 = Sign.split(",")[1];
- byte[] decodedStringFace = Base64.decode(Face2, Base64.DEFAULT);
- Bitmap decodedByteFace = BitmapFactory.decodeByteArray(decodedStringFace, 0, decodedStringFace.length);
- faceImg.setImageBitmap(decodedByteFace);
- byte[] decodedStringSign = Base64.decode(Sign2, Base64.DEFAULT);
- Bitmap decodedByteSign = BitmapFactory.decodeByteArray(decodedStringSign, 0, decodedStringSign.length);
- signImg.setImageBitmap(decodedByteSign);
- }else {
- try {
- JSONObject jObjError = new JSONObject(response.errorBody().string());
- Toast.makeText(Scan_IDAsync.this, jObjError.getString("message"), Toast.LENGTH_LONG).show();
- } catch (Exception e) {
- Toast.makeText(Scan_IDAsync.this, e.getMessage(), Toast.LENGTH_LONG).show();
- Log.e("msg", e.getMessage());
- }
- }
- }
- @Override
- public void onFailure(Call<Status> call, Throwable t) {
- Log.e("msg", t.getMessage());
- Toast.makeText(getApplicationContext(), "There is connection problem!!!", Toast.LENGTH_LONG).show();
- call.cancel();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement