package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.*;
import com.chaquo.python.Kwarg;
import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;
public class MainActivity extends AppCompatActivity {
private static int IMAGE_REQUEST_CODE =1;
private Button take_button;
private Button text_button;
private ImageView imageView;
private String paths;
private TextView textView;
private TextView outputText;
private EditText inputText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);
outputText = (TextView)findViewById(R.id.OutputText);
inputText = (EditText)findViewById(R.id.InputText);
take_button = (Button)findViewById(R.id.take_button);
text_button = (Button)findViewById(R.id.text_button);
imageView = (ImageView)findViewById(R.id.ivView);
take_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getPhoto();
}
});
text_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textRecognition();
}
});
initPython();
}
private void textRecognition(){
try {
Python py = Python.getInstance();
PyObject obj = py.getModule("main").callAttr("str_test",new Kwarg("tiles_str",inputText.getText().toString()));
String text = obj.toJava(String.class);
outputText.setText(text);
}catch (Exception e){
e.printStackTrace();
outputText.setText("输入的参数应为14个");
}
}
private void getPhoto(){
//在这里跳转到手机系统相册里面
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, IMAGE_REQUEST_CODE);
}
// 初始化Python环境
private void initPython(){
if (! Python.isStarted()) {
Python.start(new AndroidPlatform(this));
}
}
// private Bytes textTest(String p){
//
//
// return py_photo;
// }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMAGE_REQUEST_CODE && resultCode == RESULT_OK) {
try {
Uri selectedImage = data.getData(); //获取系统返回的照片的Uri
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);//从系统表中查询指定Uri对应的照片
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
paths = cursor.getString(columnIndex); //获取照片路径
cursor.close();
// PyObject obj = py.getModule("process").callAttr("test",new Kwarg("a",3));
// Integer a = obj.toJava(Integer.class);
// textView.setText(a+"");
Python py = Python.getInstance();
PyObject obj = py.getModule("process").callAttr("photo",new Kwarg("jpg",paths));
byte[] py_photo =obj.toJava(byte[].class);
int len=py_photo.length;
Bitmap bitmap = BitmapFactory.decodeByteArray(py_photo, 0, len);
imageView.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
评论0
最新资源