团队app十日冲刺8

上传图片进行检测太麻烦,现在改为直接拍照上传。

通过点击按钮来进行调用手机摄像头

private void openCamera() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}

拍完照片后,应用程序会捕获保存拍摄的照片。将这张照片转换为Base64编码的字符串,以便在网络上传输。

private String bitmapToBase64(Bitmap bitmap) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
获取token
private void getAccessTokenAsync() {
executorService.execute(() -> {
this.accessToken = getAccessToken();
handler.post(() -> {
if (this.accessToken == null) {
Log.e("MainActivity", "Failed to get access token");
}
});
});
}
private String getAccessToken() {
String authHost = "https://aip.baidubce.com/oauth/2.0/token?";
RequestBody body = new FormBody.Builder()
.add("grant_type", "client_credentials")
.add("client_id", BAIDU_API_KEY)
.add("client_secret", BAIDU_SECRET_KEY)
.build();

Request request = new Request.Builder()
.url(authHost)
.post(body)
.build();

try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
String responseBody = response.body().string();
JsonObject jsonObject = new Gson().fromJson(responseBody, JsonObject.class);
return jsonObject.get("access_token").getAsString();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
posted @ 2024-04-27 00:09  涨涨涨张  阅读(2)  评论(0编辑  收藏  举报