0

Its been a lot of days i am trying to insert the biometric fingerprint scanned data into MS SQL Server database using android through PHP.

I am sharing my entire code this time , i have to complete it anyhow. Please friends help me out solving this.

Android Code For Calling the Api:

 public void registerFinger(Bitmap scannedImage) {

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    scannedImage.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    byte[] fingerByte = byteArrayOutputStream.toByteArray();
    String encodedFingerData = Base64.encodeToString(fingerByte, Base64.DEFAULT);

    try {

        h = new JSONObject();
        h.put("FingerPrint", encodedFingerData);

    } catch (Exception e) {
        e.printStackTrace();
    }

    Call<UniversalPojo> call = apiInterfaces.registerFingerPrint(String.valueOf(empId), getRequestBody(h.toString()));

    call.enqueue(new Callback<UniversalPojo>() {
        @Override
        public void onResponse(Call<UniversalPojo> call, Response<UniversalPojo> response) {

                if (response.isSuccessful()) {

                    if (response.body().getStatus().equalsIgnoreCase("true")) {

                        showDialog("Registration Done.");

                    } else {


                         showDialog("Registration Failed.");

                    }

                } else {


                    showDialog("Registration Failed.");

                }

        }

        @Override
        public void onFailure(Call<UniversalPojo> call, Throwable t) {

             t.printStackTrace();
             showDialog("Registration Failed.");
        }
    });

}

 public RequestBody getRequestBody(String rawString) {

    return RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), rawString.toString());

 }

RegisterFingerData.php

<?php

require_once 'db_functions.php';
$db = new db_functions();

$data = json_decode(file_get_contents('php://input'), true);

$FingerPrint = $data['FingerPrint'];

//decoding and converting the image into byte array
$a = base64_decode($FingerData);
$b = array();
foreach (str_split($a) as $c) {
$b[] = sprintf("%08b", ord($c));

}

   $ifAlreadyExists = $db->registerFinger( $b );

?>

The column FingerData in EMPFINGERMASTER is of the varbinary(MAX) datatype.

registerFinger function

 public function registerFinger($fingerPrint)
 {

    try {

        $sqlString = "INSERT INTO EMPFINGERMASTER( FingerData ) values (?)";
        $stmt   = $this->conn->prepare($sqlString);
        $params = array($fingerPrint);
        $stmt->execute($params);
        $row = $stmt->fetch();

        if (!$stmt) {

            $arr = $stmt->errorInfo();
            print_r($arr);

        }

        if ($rows) {
            echo true;
        } else {
            echo false;
        }

    } catch (Exception $e) {
        echo 'Message: ' . $e->getMessage();
    }

}

GetFingerPrintData() function

 public function GetFingerPrintData($myId){

    $sqlString = "SELECT FingerData from EMPFINGERMASTER WHERE MyId = 1";
    $stmt      = $this->conn->prepare($sqlString);
    $params    = array($myId);
    $stmt->execute($params);
    $row = $stmt->fetch();

    if (!$stmt) {

        $arr = $stmt->errorInfo();
        print_r($arr);

    }

    if ($row) {

        //reconverting the byte[] array to BASE64 String
        $rawResult = $row['FingerData'];
        $resim     = substr($rawResult, 2);
        $bin       = hex2bin($rawResult);

        $base64_image = base64_encode($bin);
        echo $base64_image;

    }

}

The data getting stored in the Database is exactly like this : [B@623a5f2

but if i check in echo , it shows a big data before before converting it into byte[] in RegisterFingerData.php

And i am not be able to get the proper data at the time of retrieving.

My Question is , I want to store the data into DB and retrieve it for checking. There are a lot of suggestions on other similar questions but after using them i have reached this final code , and still not be able to get the results.

java bee
  • 115
  • 3
  • 15
  • Sorry for having to say this, but why not first learn the basics of programming? There are so many fundamental errors in your code that I don't know where to start. What is a variable? How does a function return a value? Etc. Things like that. – KIKO Software Jul 30 '18 at 06:51
  • @KIKOSoftware : hi. You are right.. but Actualy the original code is different and neat i have posted this to solve the basic concept which i am facing error into. – java bee Jul 30 '18 at 06:58
  • Why not forget about the whole hex conversion and just store the binary image data as a blob? See: http://www.mysqltutorial.org/php-mysql-blob – KIKO Software Jul 30 '18 at 07:05
  • @KIKOSoftware : I am not using mysql that is the issue here. Till date i have used mysql this time need it in MS SQL Server. – java bee Jul 30 '18 at 07:08
  • Good point, I'm so used to the combination PHP-MySQL that I overlooked that. – KIKO Software Jul 30 '18 at 07:11

0 Answers0