develop
z15755800224@163.com 3 years ago
parent 9e917e6b8b
commit 3d01465ced

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

@ -24,7 +24,6 @@
</activity>
<activity android:name=".second"/>
<activity android:name=".third"/>
<activity android:name=".four"/>
<activity android:name=".collection"/>
<provider

@ -0,0 +1,87 @@
package com.baidu.ai.aip;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
/**
* token
*/
public class AuthService {
/**
* token
* @return
* {
* "access_token": "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567",
* "expires_in": 2592000
* }
*/
public static String getAuth() {
// 官网获取的 API Key 更新为你注册的
// String clientId = "百度云应用的AK";
String clientId = "6H2WBMm7uYGYD2dKqH46EMrM";
// 官网获取的 Secret Key 更新为你注册的
// String clientSecret = "百度云应用的SK";
String clientSecret = "BoSgG8lqIcHfprxnNa2saHiwI5x3o1vP";
System.out.println(getAuth(clientId, clientSecret));
return getAuth(clientId, clientSecret);
}
/**
* API访token
* token.
* @param ak - API Key
* @param sk - Securet Key
* @return assess_token
* "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567"
*/
public static String getAuth(String ak, String sk) {
// 获取token地址
String authHost = "https://aip.baidubce.com/oauth/2.0/token?";
String getAccessTokenUrl = authHost
// 1. grant_type为固定参数
+ "grant_type=client_credentials"
// 2. 官网获取的 API Key
+ "&client_id=" + ak
// 3. 官网获取的 Secret Key
+ "&client_secret=" + sk;
try {
URL realUrl = new URL(getAccessTokenUrl);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
connection.setRequestMethod("GET");
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.err.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String result = "";
String line;
while ((line = in.readLine()) != null) {
result += line;
}
/**
*
*/
System.err.println("result:" + result);
JSONObject jsonObject = new JSONObject(result);
String access_token = jsonObject.getString("access_token");
return access_token;
} catch (Exception e) {
System.err.printf("获取token失败");
e.printStackTrace(System.err);
}
return null;
}
}

@ -20,7 +20,7 @@ public class ImgCombination {
* https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
*
*/
public static String imgCombination() {
public String imgCombination() {
// 请求url
String url = "https://aip.baidubce.com/api/v1/solution/direct/imagerecognition/combination";
try {

@ -0,0 +1,54 @@
package com.baidu.ai.aip;
import com.baidu.ai.aip.utils.Base64Util;
import com.baidu.ai.aip.utils.FileUtil;
import com.baidu.ai.aip.utils.HttpUtil;
import java.net.URLEncoder;
/**
*
*/
public class Ingredient {
/**
*
* FileUtil,Base64Util,HttpUtil,GsonUtils
* https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
* https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
* https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
* https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
*
*/
public static String ingredient() {
// 请求url
String url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/classify/ingredient";
try {
// 本地文件路径
String filePath = "D:/li.jpg";
byte[] imgData = FileUtil.readFileByBytes(filePath);
String imgStr = Base64Util.encode(imgData);
String imgParam = URLEncoder.encode(imgStr, "UTF-8");
String param = "image=" + imgParam;
// 注意这里仅为了简化编码每一次请求都去获取access_token线上环境access_token有过期时间 客户端可自行缓存,过期后重新获取。
String accessToken = "24.bb5baa6f79502180928d105dd8ea46c8.2592000.1668576873.282335-27944955";
// AuthService auth = new AuthService();
// String accessToken = auth.getAuth();
String result = HttpUtil.post(url, accessToken, param);
System.out.println(result);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
Ingredient.ingredient();
}
}

@ -3,18 +3,15 @@ package com.baidu.ai.aip;
import com.baidu.ai.aip.ImgCombination;
import com.example.ceshi.four;
public class ToIngredient extends ImgCombination{
public class ToIngredient{
//public static void main(String[] args) {
public static String x(){
public static void main(String[] args) {
ImgCombination img=new ImgCombination();
String str = img.imgCombination();
String name=str.substring(str.indexOf("name")+7,str.indexOf("\"}"));
String odds=str.substring(str.indexOf("score")+7,str.indexOf(","));
String name_odds="有"+odds.substring(0,4)+"的概率是"+name;
return name_odds;
System.out.println("有"+odds.substring(0,4)+"的概率是"+name);
}
//System.out.println("有"+odds.substring(0,4)+"的概率是"+name);
//}
}

@ -15,7 +15,7 @@ public class first extends AppCompatActivity {
//获取按钮
Button button = findViewById(R.id.button);
Button button2 = findViewById(R.id.button2);
// Button button2 = findViewById(R.id.button2);
Button button3 = findViewById(R.id.button3);
Button button4 = findViewById(R.id.button4);
//按钮进行监听
@ -29,16 +29,16 @@ public class first extends AppCompatActivity {
startActivity(intent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//监听按钮,如果点击,就跳转
Intent intent = new Intent();
//前一个MainActivity.this是目前页面后面一个是要跳转的下一个页面
intent.setClass(first.this,third.class);
startActivity(intent);
}
});
// button2.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// //监听按钮,如果点击,就跳转
// Intent intent = new Intent();
// //前一个MainActivity.this是目前页面后面一个是要跳转的下一个页面
// intent.setClass(first.this,third.class);
// startActivity(intent);
// }
// });
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

@ -79,13 +79,13 @@ public class four extends AppCompatActivity {
//ToIngredient to_in=new ToIngredient();
// ImgCombination img1=new ImgCombination();
// String str1 =img1.imgCombination();
ImgCombination img1=new ImgCombination();
String str1 =img1.imgCombination();
//// ToIngredient ii= new ToIngredient();
// String aa=ii.x();
//String name=str.substring(str.indexOf("name")+7,str.indexOf("\"}"));
TextView t1=findViewById(R.id.textView11);
t1.setText("*"+ImgCombination.imgCombination()+"A");
t1.setText(str1);
}catch (FileNotFoundException e){
e.printStackTrace();
}

@ -1,15 +0,0 @@
package com.example.ceshi;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class third extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thirdview);
}
}

@ -9,17 +9,16 @@
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/one"
android:layout_width="200dp"
android:layout_height="100dp"
android:text="果识"
android:textSize="75dp"
/>
<TextView
android:id="@+id/one"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_marginStart="137dp"
android:layout_marginTop="50dp"
android:text="果识"
android:textSize="75dp" />
<Button
android:id="@+id/button3"
@ -28,30 +27,23 @@
tools:ignore="MissingConstraints"
android:text="水果鉴定"
android:layout_below="@+id/one"
android:layout_marginTop="343dp" app:backgroundTint="#4CAF50"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="50dp"
tools:ignore="MissingConstraints"
android:text="水果处理"
android:layout_below="@+id/one"
android:layout_marginTop="267dp" app:backgroundTint="#4CAF50"/>
<Button
android:layout_marginTop="170dp" app:backgroundTint="#4CAF50"/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="50dp"
tools:ignore="MissingConstraints"
android:text="水果百科"
android:layout_below="@+id/one"
android:layout_marginTop="188dp" app:backgroundTint="#4CAF50"/>
android:layout_marginTop="100dp" app:backgroundTint="#4CAF50"/>
<Button
android:text="我的收藏"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/button4" app:backgroundTint="#FFB13B"
android:insetBottom="6dp"
android:layout_marginTop="520dp"/>
android:layout_below="@+id/one"
android:layout_marginTop="240dp"/>
</RelativeLayout>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

@ -1,26 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".first">
<TextView
android:id="@+id/five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="水果处理"
android:textSize="25dp"
android:textColor="#663399"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<SearchView
android:layout_width="408dp"
android:layout_height="96dp"
android:background="@drawable/green" tools:layout_editor_absoluteY="1dp" tools:layout_editor_absoluteX="2dp"
tools:ignore="MissingConstraints"/>
</androidx.constraintlayout.widget.ConstraintLayout>

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB

@ -2,15 +2,15 @@
<!-- Base application theme. -->
<style name="Theme.Ceshi" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorPrimary">#B792FA</item>
<item name="colorPrimaryVariant">#BFAFE1</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorSecondary">#98D6D0</item>
<item name="colorSecondaryVariant">#58C1C0</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<item name="android:statusBarColor" tools:targetApi="l">#CCBBF1</item>
<!-- Customize your theme here. -->
</style>
</resources>
Loading…
Cancel
Save