Android захватывает изображение и передает результат деятельности - PullRequest
0 голосов
/ 03 января 2019

Прямо сейчас у меня есть этот код.

CameraActivity.class

private PictureCallback getPictureCallback() {
    PictureCallback picture = new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            //make a new picture file
            File pictureFile = null;
            try {
                pictureFile = createImageFile();
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (pictureFile == null) {
                return;
            }

            Intent returnIntent = new Intent();
            returnIntent.putExtra("cameraresult", imagePath);
            setResult(Activity.RESULT_OK, returnIntent);
            finish();
            //refresh camera to continue preview
            //mPreview.refreshCamera(mCamera);
        }
    };
    return picture;
}

OnClickListener captrureListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        mCamera.autoFocus(new Camera.AutoFocusCallback() {
            @Override
            public void onAutoFocus(boolean success, Camera camera) {
                mCamera.takePicture(null, null, mPicture);
            }
        });

    }
};

//make picture and save to a folder

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "TIMEKEEPER_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    imagePath =  image.getAbsolutePath();
    return image;
}

И MainActivity.class

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

    // 카메라 결과
    else if(requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
        try {

            String cameraresult = data.getStringExtra("cameraresult");
            Uri imageUri = Uri.fromFile(new File(cameraresult));
            //final Uri imageUri = photoURI;
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            final InputStream imageStream = getContentResolver().openInputStream(imageUri);
            final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
            **selectedImage.compress(Bitmap.CompressFormat.JPEG, 60, stream);**
            byte[] byteFormat = stream.toByteArray();
            String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP);
            String image = "data:image/jpeg;base64," + imgString;
            selectedImage.recycle();

Я пытаюсь сделать этоCameraActive отправляет путь к файлу снятого изображения, и основная активность превращает путь в URI и кодирует в Base64.Но в классе MainActivity.class часть selectedImage.compress (Bitmap.CompressFormat.JPEG, 60, stream); ** имеет ошибку.Я не могу понять, что вызывает это.Кто-нибудь может мне помочь?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...