parent
b70fc2cadc
commit
b81883878b
@ -0,0 +1,173 @@
|
||||
package recognition;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.database.Cursor;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.provider.MediaStore;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.example.musicwork.MainActivity;
|
||||
import com.example.musicwork.R;
|
||||
import com.example.musicwork.freeModel.FileServiceUpload;
|
||||
import com.example.musicwork.freeModel.FreemdActivity;
|
||||
import com.example.musicwork.freeModel.InitActivity;
|
||||
import com.example.musicwork.freeModel.MusicNote;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MusicScoreRecognition extends AppCompatActivity {
|
||||
String imageName;//音乐名
|
||||
String filepath;
|
||||
String filename;
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_music_score_recognition);
|
||||
Button button_toFree = (Button) findViewById(R.id.uploadimage);
|
||||
button_toFree.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
//Intent intent = new Intent(MusicScoreRecognition.this, InitActivity.class);//进入自由模式
|
||||
//启动
|
||||
selectImg();
|
||||
verifyStoragePermissions();
|
||||
|
||||
try {
|
||||
File file = new File(Environment.getExternalStorageDirectory().getPath(),"/DCIM/Camera/2.jpg");
|
||||
|
||||
if (!file.exists())//如果不存在该文件
|
||||
{
|
||||
file.createNewFile();
|
||||
}
|
||||
|
||||
filepath=file.getPath();
|
||||
filename="2.jpg";
|
||||
/* if(Image.uploadImage(filepath,filename)){
|
||||
Toast.makeText(getApplicationContext(),"保存成功",Toast.LENGTH_SHORT).show();
|
||||
}else{
|
||||
Toast.makeText(getApplicationContext(),"保存失败",Toast.LENGTH_SHORT).show();
|
||||
}*/
|
||||
|
||||
//Log.e("map",""+file.getPath());
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
Button display = (Button)findViewById(R.id.display);
|
||||
display.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
try {
|
||||
sentFile("fly.txt");
|
||||
}catch(IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public void selectImg() {//选择图片
|
||||
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
|
||||
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
|
||||
startActivityForResult(intent, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {//点击图片后进行处理
|
||||
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
//在相册里面选择好相片之后调回到现在的这个activity中
|
||||
switch (requestCode) {
|
||||
case 1:
|
||||
if (resultCode == RESULT_OK) {//resultcode是setResult里面设置的code值
|
||||
try {
|
||||
Toast.makeText(getApplicationContext(),"上传成功",Toast.LENGTH_SHORT).show();
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generatedcatch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static final int REQUEST_EXTERNAL_STORAGE = 1;
|
||||
private static String[] PERMISSIONS_STORAGE = {
|
||||
"android.permission.READ_EXTERNAL_STORAGE",
|
||||
"android.permission.WRITE_EXTERNAL_STORAGE" };
|
||||
public void verifyStoragePermissions() {
|
||||
try {
|
||||
//检测是否有写的权限
|
||||
int permission = ActivityCompat.checkSelfPermission(MusicScoreRecognition.this,
|
||||
"android.permission.WRITE_EXTERNAL_STORAGE");
|
||||
if (permission != PackageManager.PERMISSION_GRANTED) {
|
||||
// 没有写的权限,去申请写的权限,会弹出对话框
|
||||
ActivityCompat.requestPermissions(MusicScoreRecognition.this, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//将文件内容传入主活动
|
||||
public void sentFile(String filename) throws IOException {
|
||||
verifyStoragePermissions();
|
||||
|
||||
Bundle bundle = new Bundle();
|
||||
Intent intent = new Intent(MusicScoreRecognition.this, FreemdActivity.class);//传递输入数据
|
||||
bundle.putString("flag","open");
|
||||
|
||||
List<MusicNote> noteList = new ArrayList<>();
|
||||
String path= Environment.getExternalStorageDirectory().getPath()+"/Music/"+filename;
|
||||
String environment = Environment.getExternalStorageState();
|
||||
if(Environment.MEDIA_MOUNTED.equals(environment)) {
|
||||
//读文件
|
||||
File file = new File(path);
|
||||
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
Log.e("filepath:", file.getPath());
|
||||
DataInputStream in = new DataInputStream(fis);
|
||||
//StringBuilder content = new StringBuilder();
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
|
||||
String line;
|
||||
String mydata = "";
|
||||
int i = 0;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
String[] strarray = line.split(" ");
|
||||
bundle.putStringArray("note"+i,strarray);
|
||||
i += 1;
|
||||
}
|
||||
bundle.putInt("length",i);//存储读出的音符列表的长度
|
||||
}
|
||||
//将数据发送到主活动
|
||||
intent.putExtras(bundle);
|
||||
startActivity(intent);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue