很多天以来,我一直在尝试通过android通过PHP将生物指纹扫描数据插入MS SQL Server数据库。
这次我要共享我的全部代码,无论如何我都必须完成它。 请朋友帮我解决这个问题。
用于调用Api的Android代码 :
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 );
?>
EMPFINGERMASTER 中的列 FingerData 属于 varbinary(MAX)数据类型。
registerFinger函数
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()函数
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;
}
}
要存储在数据库中的数据完全是这样的: [B @ 623a5f2
但是如果我签入echo,它将显示大数据,然后再将其转换为RegisterFingerData.php中的byte []
在检索时,我无法获得正确的数据。
我的问题是,我想将数据存储到DB中并检索它以进行检查。 关于其他类似问题,还有很多建议,但是使用它们后,我到达了最终代码,仍然无法获得结果。