Как получить строку BitMap, Bytearray или Base64 с камеры - PullRequest
0 голосов
/ 16 ноября 2018

Мне нужно получить растровое изображение, байтовый массив или строку base64 изображения, которое я сделал с помощью камеры Android.

Чтобы начать захват, я использую это.

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            // Ensure that there's a camera activity to handle the intent
            if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                // Create the File where the photo should go
                File photoFile = null;
                try {
                    photoFile = createImageFile();
                } catch (IOException ex) {
                    // Error occurred while creating the File

                }
                // Continue only if the File was successfully created
                if (photoFile != null) {
                    Uri photoURI = FileProvider.getUriForFile(getApplicationContext(),
                            "com.myapp.app.fileprovider",
                            photoFile);
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                    startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
                }
            }

Я могу сохранить фотографию в галерею, используя эту

private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + 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
        mCurrentPhotoPath = image.getAbsolutePath();
        return image;
    }

И это то, что я пробовал до сих пор, но переменная растрового изображения, кажется, остается нулевой.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

        /*if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            mImageView.setVisibility(View.VISIBLE);
            mImageView.setImageBitmap(imageBitmap);
        }*/
    try {
        switch (requestCode) {

            case 1: {
                if (resultCode == RESULT_OK) {

                    File file = new File(mCurrentPhotoPath);
                    Toast.makeText(this, mCurrentPhotoPath, Toast.LENGTH_SHORT).show();
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), Uri.fromFile(file));
                    if (bitmap != null) {

                        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
                        byte[] byteArray = byteArrayOutputStream.toByteArray();

                        String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
                        Base = encoded;

                        if (Base != "") {
                            mCheck.setChecked(true);
                        }
                    }
                }
                break;
            }
        }

    } catch (Exception error) {
        error.printStackTrace();
    }

Любая помощь будет принята с благодарностью.

...