将图像捕获到三个 ImageView 中

Capture image into three ImageViews

在同一个 Activity 上,我有三张卡片,每张卡片都有一个 ImageButton 和一个 ImageView,如下所示:

按下相机按钮时,我希望相机拍照并将拍摄的图像分配给特定的 ImageView。 我到目前为止是这样的:

我像这样为每个按钮创建了一个 OnTouchEventListener:

ImageButton camera1 = (ImageButton) findViewById(R.id.camera_button1);
        camera1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {

                    case MotionEvent.ACTION_DOWN:

                        // action started modify alpha
                        v.setAlpha((float)0.5);
                        break;

                    case MotionEvent.ACTION_UP:

                        // touch ended modify alpha back
                        // Toast.makeText(context, "Touch ended", Toast.LENGTH_SHORT).show();
                        v.setAlpha((float)1.0);

                        // create Intent to take a picture and return control to the calling application
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        //intent.putExtra("imageViewName", "front");

                        //bundle.putString("camera1", "front");
                        Bundle bundle = new Bundle();
                        bundle.putString("camera1", "front");

                        // start the image capture Intent
                        startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

                        break;

                    default:
                        break;

                }

                return false;
            }
        });

都不是,使用 intent.putExtra("test", "test");或 intent.putExtra(bundle) 工作。我向每个意图添加一个字符串的原因是能够将该字符串用作标志并在:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {

                Bundle bundle = null;
                bundle = getIntent().getExtras();

                // Get the bundle and extract strings
                String test = bundle.getString("camera1");

                // Create a thumbnail
                Bitmap thumbnail = (Bitmap) data.getExtras().get("data");

                // Detect where to assign the view
                switch ((String)data.getExtras().get("imageViewName")) {

                    case "front":
                        front.setImageBitmap(thumbnail);
                        break;
                    case "message":
                        message.setImageBitmap(thumbnail);
                        break;
                    case "address":
                        address.setImageBitmap(thumbnail);
                        break;
                    case "other1":
                        other1.setImageBitmap(thumbnail);
                        break;
                    case "other2":
                        other2.setImageBitmap(thumbnail);
                        break;
                    case "other3":
                        other3.setImageBitmap(thumbnail);
                        break;

                    default:
                        break;
                }

                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
                try {
                    file.createNewFile();
                    FileOutputStream fo = new FileOutputStream(file);
                    fo.write(bytes.toByteArray());
                    fo.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else if (resultCode == RESULT_CANCELED) {
                // User cancelled the image capture
            } else {
                // Image capture failed, advise user

            }
        }

        // Get the return data from the camera

    }

尝试抓住那个意图并选择哪个 ImageView 来分配从相机推送的图像。所以基本上,我希望能够在用户单击 ImageView1 的按钮时让图像从相机返回并附加到该 ImageView。我该如何处理?

选项一:

将不同的常量传递给您的相机 activity,例如

startActivityForResult(it, REQ_SCAN + imgIndex);

然后在你的

中抓住它
 @Override
  protected void onActivityResult(int request, int result, Intent data) {
    switch (request) {
      case REQ_SCAN + 0: 
      case REQ_SCAN + 1: 
      case REQ_SCAN + 2:  break;
      imgIndex = request - REQ_SCAN;
    }
   ...

这三个不同的值应该可以根据您的要求唯一地标识您的照片。

选项二:

启动相机时,创建一个具有唯一名称的临时文件,将其交给相机并保留名称。

      tmpFl = File.createTempFile(TMP_FILENM, null, context.getExternalCacheDir());
      it.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tmpFl));
        startActivityForResult(it, REQ_SCAN);
        mTmpFlNm = tmpFl.getAbsolutePath();
      }

当它在 'onActivityResult()'

中返回时
  case REQ_SCAN: {
    if (result == Activity.RESULT_OK) {
      try {
        tmpFl = new File(mTmpFlNm);
        byte[] jpgBuff = UT.file2Bytes(tmpFl));
      } finally { if (tmpFl != null) tmpFl.delete(); }
    }
    break;  

只需获取您交给摄像头的临时文件 activity,获取它的内容(它是一个包含 jpg 数据的缓冲区)并将其解压缩为位图(或用它做其他事情 - 上传、重命名, 保存...).

因此,在您的情况下,您可能会使用 3 个不同的文件名。选项一可能更适合您的需求。

祝你好运