1

我完全是关于 android 的新闻,不幸的是,很少有时间以正确的方式学习它,我有一个工作要发布。问题是:我需要拍张照片并用我制作的算法处理她。我用我能找到的最简单的方法做到了,我知道对于那些真正获得 android 的人来说,这看起来真的很糟糕(抱歉)

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    takePic();

protected void takePic(){
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(takePictureIntent, 100);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     Bundle extras = data.getExtras();
     mImageBitmap = (Bitmap) extras.get("data");
             Algorithm(mImageBitmap)

但它没有处理,它拍了一张照片,要求保存或取消并离开应用程序,我已经通过不同的方式(创建一个新活动),但似乎没有任何工作

4

1 回答 1

0

这是我是怎么做到的

前往相机:

在某个地方,声明一个 fileUri 变量并保留它

Uri fileUri;
final int TAKE_PICTURE=100;//this can be any int, really
public void goToCamera(){
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo;
    try
    {
        // place where to store camera taken picture
        photo = this.createTemporaryFile("picture", ".jpg");
        Log.v(TAG, "Here(after createTempFile)");
        photo.delete();
    }
    catch(Exception e)
    {
       Log.v(TAG, "Can't create file to take picture!" + e.getMessage());
       Toast.makeText(context, "Please check SD card!", Toast.LENGTH_SHORT).show();
       return; 
    }
    fileUri = Uri.fromFile(photo);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    //Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);   
    startActivityForResult(intent, TAKE_PICTURE);
}

然后检索图像

protected void onActivityResult(int requestCode, int resultCode, Intent data){

    if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK){
       this.getContentResolver().notifyChange(uri, null);
       ContentResolver cr = this.getContentResolver();
       Bitmap bitmap;
       try
       {
           BitmapFactory.Options ops = new BitmapFactory.Options();
           ops.inSampleSize = 4;
           bitmap = BitmapFactory.decodeFile(uri.getPath().toString(), ops);
       }
       catch (Exception e)
       {
           Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
           Log.d(TAG, "Failed to load", e);
       }
    }
}

上面提到的创建临时文件:

private File createTemporaryFile(String part, String ext) throws Exception
{
    File tempDir= Environment.getExternalStorageDirectory();
    tempDir=new File(tempDir.getAbsolutePath()+"/.temp/");
    Log.i(TAG, tempDir.toString());
    if(!tempDir.exists())
    {
        Log.i(TAG, "Dir doesnt exist");
        tempDir.mkdirs();
    }
    return File.createTempFile(part, ext, tempDir);
}

我意识到这可能不像您希望的那么简单,但是这种方法似乎尽可能灵活和兼容。让我知道我是否遗漏了其他任何内容

于 2013-07-24T17:12:01.217 回答