簡體   English   中英

如何將捕獲的圖像從自定義相機傳遞到另一個活動?

[英]How to pass a captured image from custom camera to another activity?

我目前正在嘗試構建一個自定義相機應用程序,該應用程序將捕獲圖像,然后將其顯示在另一個活動中(通過更新 ImageView)。

目前,我可以通過我的相機預覽在設備上成功捕獲和保存圖像,甚至可以使用捕獲的圖像成功更新該活動中的 ImageView - 但是,我在將捕獲的圖像傳遞給第二個活動。

我在網上看到很多結合使用 ActivityResult 和 Intent 的解決方案,但它們似乎都使用設備的本機相機應用程序,而不是像我這樣使用自定義相機。

我的 CameraActivity 類如下所示:

@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera_activity);
    swatch = (ImageView) findViewById(R.id.swatch);
    preview = (FrameLayout) findViewById(R.id.camera_preview);
    cameraButton = (Button) findViewById(R.id.captureButton);


    cameraButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            mCamera.takePicture(null, null, mPicture);

        }
    });
}

private Camera.PictureCallback mPicture = new Camera.PictureCallback() {

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        if (pictureFile == null) {
            Log.d("Camera error",
                    "Error creating media file, check storage permissions: ");
            return;
        }
        Log.i("Picture", pictureFile.toString());
        try {

            final ImageView imageView = (ImageView) findViewById(R.id.imageView);
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
            BitmapFactory.Options scalingOptions = new BitmapFactory.Options();
            scalingOptions.inSampleSize = camera.getParameters().getPictureSize().width / imageView.getMeasuredWidth();
            final Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length, scalingOptions);

        } catch (FileNotFoundException e) {
            Log.d("Camera error", "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d("Camera error", "Error accessing file: " + e.getMessage());
        }

        finish();
        openColourDetectorActivity();
    }
};

private static File getOutputMediaFile(int type) {

    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
            "Medici");
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.i("Medici", "failed to create directory");
            return null;
        }
    }

    @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyymmddhhmmss")
            .format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;
}


public void openColourDetectorActivity() {
    Intent intent = new Intent(this, ColourDetectorActivity.class);
    startActivity(intent);
}

當然,我試圖在該活動的 onCreate 方法的另一端接收它。

知道我該怎么做嗎?

openColourDetectorActivity(pictureFile);

public void openColourDetectorActivity(File file) {
    Intent intent = new Intent(this, ColourDetectorActivity.class);
    intent.putExtra("FILE", file.getPath());
    startActivity(intent);
}

在 ColorDetectorActivity 中:

new File(getIntent().getStringExtra("FILE"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM