|
|
|
@ -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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|