package com.example.ceshi;


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.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import com.baidu.ai.aip.ImgCombination;
import com.baidu.ai.aip.ToIngredient;
import com.baidu.ai.aip.Ingredient;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;



public class four extends AppCompatActivity {
    final int TAKE_PHOTO=1;
    ImageView iv_photo;
    Uri imageUri;
    TextView t1;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fourview);

        Button 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) {
                File output=new File(getExternalCacheDir(),"output_image.jpg");
                try {
                    if (output.exists()){
                        output.delete();
                    }
                    output.createNewFile();
                }catch (IOException e){
                    e.printStackTrace();
                }
                if (Build.VERSION.SDK_INT>=24){
//图片的保存路径
                    imageUri= FileProvider.getUriForFile(four.this,"com.example.takephoto.fileprovider",output);
                }
                else { imageUri=Uri.fromFile(output);}
                //跳转界面到系统自带的拍照界面
                Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
                intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
                startActivityForResult(intent,TAKE_PHOTO);
            }
        });


//        String str =  ImgCombination.imgCombination();
//        String name=str.substring(str.indexOf("name")+7,str.indexOf("\"}"));
//        String odds=str.substring(str.indexOf("score")+7,str.indexOf(","));


    }

    @Override

    protected  void onActivityResult(int requestCode,int resultCode,Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case TAKE_PHOTO:
                if (resultCode==RESULT_OK){
                    // 使用try让程序运行在内报错
                    try {
                        //将图片保存
                        Bitmap bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
                        iv_photo.setImageBitmap(bitmap);


                        //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);
                    }catch (FileNotFoundException e){
                        e.printStackTrace();
                    }
                }
                break;
            default:break;
        }
    }

}