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.