master
z15755800224@163.com 3 years ago
parent a295b4c140
commit 121282ee95

@ -29,9 +29,9 @@ android {
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation files('D:\\FruitsProject\\app\\src\\main\\res\\aip-java-sdk-4.16.9.jar')
implementation files('D:\\FruitsProject\\app\\src\\main\\res\\json-20160810.jar')
implementation files('D:\\FruitsProject\\app\\src\\main\\res\\slf4j-api-1.7.25.jar')

@ -4,6 +4,8 @@
package="com.example.ceshi">
<!--//访问电话状态-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!--//允许全部网络访问-->
<uses-permission android:name="android.permission.INTERNET" />
<!--//获取网络信息状态-->
@ -13,6 +15,11 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET" />
<!--//写入-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE\"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS\"/>
<application
android:allowBackup="true"
@ -23,10 +30,11 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Ceshi"
android:usesCleartextTraffic="true"
android:networkSecurityConfig="@xml/network_security_config"
tools:targetApi="31">
<meta-data android:name="6H2WBMm7uYGYD2dKqH46EMrM" android:value="BoSgG8lqIcHfprxnNa2saHiwI5x3o1vP">
</meta-data>
<activity
android:name=".first"
android:exported="true">

@ -23,7 +23,7 @@ public class ImgCombination {
public String imgCombinatio(){
return "QEEEEEE";
}
public String imgCombination() {
public String imgCombination() {
// 请求url
String url = "https://aip.baidubce.com/api/v1/solution/direct/imagerecognition/combination";
try {
@ -40,7 +40,7 @@ public class ImgCombination {
// 注意这里仅为了简化编码每一次请求都去获取access_token线上环境access_token有过期时间 客户端可自行缓存,过期后重新获取。
String accessToken = "24.bb5baa6f79502180928d105dd8ea46c8.2592000.1668576873.282335-27944955";
String result = HttpUtil.post(url, accessToken, "application/json", param);
String result =HttpUtil.post(url, accessToken, "application/json", param);
//System.out.println("**************************");
//System.out.println(result);
//System.out.println("**************************");
@ -48,7 +48,10 @@ public class ImgCombination {
} catch (Exception e) {
e.printStackTrace();
}
return "FORS";
return "Error1";
}
// public static void main(String[] args) {
// ImgCombination.imgCombination();
// }
}

@ -1,10 +1,13 @@
package com.baidu.ai.aip;
import android.graphics.Bitmap;
import com.baidu.ai.aip.utils.Base64Util;
import com.baidu.ai.aip.utils.FileUtil;
import com.baidu.ai.aip.utils.HttpUtil;
import java.net.URI;
import java.net.URLEncoder;
/**
@ -21,12 +24,12 @@ public class Ingredient {
* https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
*
*/
public static String ingredient() {
public String ingredient() {
// 请求url
String url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/classify/ingredient";
try {
// 本地文件路径
String filePath = "D:/li.jpg";
String filePath = "手机存储/Pictures/p2.jpg";
byte[] imgData = FileUtil.readFileByBytes(filePath);
String imgStr = Base64Util.encode(imgData);
String imgParam = URLEncoder.encode(imgStr, "UTF-8");
@ -34,7 +37,7 @@ public class Ingredient {
String param = "image=" + imgParam;
// 注意这里仅为了简化编码每一次请求都去获取access_token线上环境access_token有过期时间 客户端可自行缓存,过期后重新获取。
String accessToken = "24.bb5baa6f79502180928d105dd8ea46c8.2592000.1668576873.282335-27944955";
String accessToken = "24.29d61c398f70469aa1b17124092051de.2592000.1668944414.282335-27944955";
// AuthService auth = new AuthService();
// String accessToken = auth.getAuth();
@ -48,7 +51,7 @@ public class Ingredient {
return null;
}
public static void main(String[] args) {
Ingredient.ingredient();
}
// public static void main(String[] args) {
// Ingredient.ingredient();
// }
}

@ -59,7 +59,7 @@ public class HttpUtil {
Map<String, List<String>> headers = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : headers.keySet()) {
System.err.println(key + "--->" + headers.get(key));
//System.err.println(key + "--->" + headers.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
BufferedReader in = null;
@ -71,7 +71,7 @@ public class HttpUtil {
result += getLine;
}
in.close();
System.err.println("result:" + result);
//System.err.println("result:" + result);
return result;
}
}

@ -15,7 +15,7 @@ public class WebViewActivity extends AppCompatActivity {
setContentView(R.layout.activity_web_view);
mWebView = (WebView) findViewById(R.id.web_view);
Intent intent = getIntent();
String url = "http://www.baidu.com";//intent.getStringExtra("url");
String url = "https://wapbaike.baidu.com/";//intent.getStringExtra("url");
mWebView.loadUrl(url);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient(){

@ -1,6 +1,7 @@
package com.example.ceshi;
import android.content.Intent;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

@ -1,17 +1,21 @@
package com.example.ceshi;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
@ -22,35 +26,37 @@ import com.baidu.ai.aip.Ingredient;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class four extends AppCompatActivity {
Bitmap bitmap;
final int TAKE_PHOTO=1;
Button btn_1;
ImageView iv_photo;
Uri imageUri;
TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState){
// Android在4.0之前的版本支持在主线程中访问网络但是在4.0以后对这部分程序进行了优化,也就是说访问网络的代码不能写在主线程中了。
//主线程中进行网络请求异常, Android4.0 版本开始后就强制程序不能在主线程中访问网络, 要把访问网络放在独立的子线程中.
//在 Activity 的 onCreate() 方法中添加, 忽略这些强制策略.
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
super.onCreate(savedInstanceState);
setContentView(R.layout.fourview);
Button btn_1=findViewById(R.id.btn_takephoto);
btn_1=findViewById(R.id.btn_takephoto);
iv_photo=findViewById(R.id.img_photo);
t1=findViewById(R.id.textView11);
//ToIngredient ii= new ToIngredient();
ImgCombination img=new ImgCombination();
//String str11 = img.imgCombination();
Ingredient a=new Ingredient();
// "{\"result_num\":5,\"result\":[{\"score\":0.6307158,\"name\":\"砀山酥梨\"},{\"score\":0.19453917,\"name\":\"砀山梨\"},{\"score\":0.06309598,\"name\":\"北丰梨\"},{\"score\":0.062131744,\"name\":\"雪梨\"},{\"score\":0.0255048,\"name\":\"梨\"}],\"log_id\":1582269295467236738}\n";
//t1.setText("A"+str11+"B");
t1.setText("###"+Ingredient.ingredient());
btn_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@ -64,10 +70,11 @@ public class four extends AppCompatActivity {
e.printStackTrace();
}
if (Build.VERSION.SDK_INT>=24){
//图片的保存路径
//图片的保存路径
imageUri= FileProvider.getUriForFile(four.this,"com.example.takephoto.fileprovider",output);
}
else { imageUri=Uri.fromFile(output);}
else { imageUri=Uri.fromFile(output);
}
//跳转界面到系统自带的拍照界面
Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
@ -79,13 +86,48 @@ public class four extends AppCompatActivity {
// String str = ImgCombination.imgCombination();
// String name=str.substring(str.indexOf("name")+7,str.indexOf("\"}"));
// String odds=str.substring(str.indexOf("score")+7,str.indexOf(","));
}
public void saveImageToGallery(Context context, Bitmap bitmap) {
//检查有没有存储权限
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
Toast.makeText(this, "请至权限中心打开应用权限", Toast.LENGTH_SHORT).show();
} else {
// 新建目录appDir并把图片存到其下
File appDir = new File(context.getExternalFilesDir(null).getPath()+ "BarcodeBitmap");
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = "p2" + ".jpg";
File file = new File(appDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 把file里面的图片插入到系统相册中
try {
MediaStore.Images.Media.insertImage(context.getContentResolver(),
file.getAbsolutePath(), fileName, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Toast.makeText(this, fileName, Toast.LENGTH_LONG);
// 通知相册更新
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
}
}
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
public void onActivityResult(int requestCode,int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case TAKE_PHOTO:
@ -93,19 +135,36 @@ public class four extends AppCompatActivity {
// 使用try让程序运行在内报错
try {
//将图片保存
Bitmap bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
iv_photo.setImageBitmap(bitmap);
Ingredient qw=new Ingredient();
t1.setText("###"+qw.ingredient());
saveImageToGallery(four.this,bitmap);
//public void saveMyBitmap(Bitmap mBitmap,String bitName){\n\n
// File f = new File( "/sdcard/baidu/"+"jpg1" + ".jpg");
// FileOutputStream fOut = null;
// fOut = new FileOutputStream(f);
// bitmap.compress(Bitmap.CompressFormat.JPEG,100, fOut);
//ToIngredient to_in=new ToIngredient();
ImgCombination img1=new ImgCombination();
String str1 =img1.imgCombination();
//// ToIngredient ii= new ToIngredient();
// ToIngredient to_in=new ToIngredient();
// 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(str1);
// String name=str.substring(str.indexOf("name")+7,str.indexOf("\"}"));
// TextView t1=()findViewById(R.id.textView11);
// t1.setText(str1);
// ImgCombination img=new ImgCombination();
// String str11 = img.imgCombination();
// t1.setText("####"+str11);
}catch (FileNotFoundException e){
e.printStackTrace();
}
@ -113,6 +172,9 @@ public class four extends AppCompatActivity {
break;
default:break;
}
}
}

@ -24,7 +24,7 @@
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:text="no use"
android:layout_below="@+id/img_photo"
android:layout_marginTop="34dp" app:backgroundTint="#4CAF50"/>
/>

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
Loading…
Cancel
Save