MainActivity.java:
package com.example.dell.capcrop;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
final int CAMERA_CAPTURE=1;
private Uri picUri;
final int PIC_CROP=2;
Bundle bundle;
Bitmap bitmap;
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button captureBtn=(Button)findViewById(R.id.capture_btn);
captureBtn.setOnClickListener(this);
}
@Override
public void onClick(View v){
if (v.getId()==R.id.capture_btn){
try {
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureIntent,CAMERA_CAPTURE);
}
catch (ActivityNotFoundException anfe){
String errorMessage ="Whoops - your device doesn't support capturing image";
Toast toast=Toast.makeText(this,errorMessage,Toast.LENGTH_SHORT);
toast.show();
}
}
}
private void performCrop() {
try{
Intent cropIntent=new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri,"image/*");
cropIntent.putExtra("crop","true");
cropIntent.putExtra("aspectX",1);
cropIntent.putExtra("aspectY",1);
cropIntent.putExtra("outputX",256);
cropIntent.putExtra("outputY",256);
cropIntent.putExtra("return_data",true);
startActivityForResult(cropIntent,PIC_CROP);
}
catch (ActivityNotFoundException anfe)
{
String errorMessage="Whoops - your device doesn't support capturing image";
Toast toast=Toast.makeText(this,errorMessage,Toast.LENGTH_SHORT);
toast.show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==request&&resultCode==RESULT_OK)
{
bundle=data.getExtras();
bitmap=(Bitmap)bundle.get("data");
img.setImageBitmap(bitmap);
Thread thread=new Thread()
{
@Override
public void run() {
try {
sleep(2000);
Intent intent=new Intent(getApplicationContext(),result.class);
bundle.putParcelable("bmp",bitmap);
intent.putExtras(bundle);
startActivity(intent);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
}
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_CAPTURE) {
picUri = data.getData();
performCrop();
} else if (requestCode == PIC_CROP) {
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
ImageView picView = (ImageView) findViewById(R.id.picture);
picView.setImageBitmap(thePic);
}
}
}
}
我的应用程序基本上是手语解释器,我需要删除一个错误。这是其中存在多个onactivityresult()
定义的错误代码。我做了一些更改,但都没有用。在这段代码中,移动相机捕获图像并显示一个框架,用户可以在其中裁剪图像的所需部分并在下一个活动中显示。