首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用TextureView保存图像?

用TextureView保存图像?
EN

Stack Overflow用户
提问于 2021-03-09 10:09:48
回答 1查看 82关注 0票数 0

我希望你能解决我的问题:我有一个文本评论来拍照,但是当他捕获照片时,需要包含图片的文件是空的。我不知道在哪里有什么问题。我只是认为问题在于文件是何时创建的。谢谢你帮我。

这是我代码的一部分:

代码语言:javascript
运行
复制
private void takePicture() throws CameraAccessException, IOException {
    if(cameraDevice == null){
        return;
    }
    CameraManager manager = (CameraManager) Objects.requireNonNull(getContext()).getSystemService(Context.CAMERA_SERVICE);
    CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraDevice.getId());
    Size[] jpegSizes = null;

    jpegSizes = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP).getOutputSizes(ImageFormat.JPEG);

    int width = 640;
    int height = 480;
    if(jpegSizes!=null && jpegSizes.length>0){
        width = jpegSizes[0].getWidth();
        height = jpegSizes[0].getHeight();
    }

    ImageReader reader = ImageReader.newInstance(width,height, ImageFormat.JPEG, 1);
    List<Surface> outputSurface = new ArrayList<>(2);
    outputSurface.add(reader.getSurface());

    outputSurface.add(new Surface(textureView.getSurfaceTexture()));
    final CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
    captureBuilder.addTarget(reader.getSurface());
    captureBuilder.set(CaptureRequest.CONTROL_MODE,CameraMetadata.CONTROL_MODE_AUTO);

    int rotation = Objects.requireNonNull(getActivity()).getWindowManager().getDefaultDisplay().getRotation();
    captureBuilder.set(CaptureRequest.JPEG_ORIENTATION,ORIENTATIONS.get(rotation));

    // First I get the path to gallery and crate new Album to my app

    File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
    mImageFolder = new File(file, "Fluico");
    if (!mImageFolder.exists()) {
        if (!mImageFolder.mkdirs()) {
            Log.d("Fluicophoto", "failed to create directory");
        }
    }

    /*Second I cut mFile = new File(getActivity().getExternalFilesDir(null), "pic.jpg");
    from onActivityCreated and add here with the new path from my Album*/

    @SuppressLint("SimpleDateFormat") String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String prepend = "IMAGE_" + timestamp + "_";
    file = File.createTempFile(prepend, ".jpg", mImageFolder);
    Toast.makeText(getContext(), "file need to be save", Toast.LENGTH_SHORT).show();

    ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
        @Override
        public void onImageAvailable(ImageReader reader) {
            Image image = null;

            image = reader.acquireLatestImage();
            ByteBuffer buffer = image.getPlanes()[0].getBuffer();
            byte[] bytes = new byte[buffer.capacity()];
            buffer.get(bytes);
            try {
                save(bytes);
            } finally {
                image.close();
            }
        }
    };

    reader.setOnImageAvailableListener(readerListener,mBackgroundHandler);

    final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() {
        @Override
        public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
            super.onCaptureCompleted(session, request, result);
            Toast.makeText(getContext(), "saved", Toast.LENGTH_SHORT).show();
            try{
                createCameraPreview();
            }catch (CameraAccessException e){
                Toast.makeText(getContext(), "capture not comleted", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
        }
    };

    cameraDevice.createCaptureSession(outputSurface, new CameraCaptureSession.StateCallback() {
        @Override
        public void onConfigured( CameraCaptureSession session) {
            try {
                session.capture(captureBuilder.build(), captureListener, mBackgroundHandler);
            } catch (CameraAccessException e) {
                e.printStackTrace();
                Toast.makeText(getContext(), "capture not configured", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onConfigureFailed( CameraCaptureSession session) {

        }
    },mBackgroundHandler);
}

private void save(byte[] bytes) {
    OutputStream outputStream = null;
    try {
        outputStream = new FileOutputStream(file);
        outputStream.write(bytes);
        outputStream.close();
        Toast.makeText(getContext(), "it's good", Toast.LENGTH_SHORT).show();
    }catch (IOException e){
        Toast.makeText(getContext(), "file not found", Toast.LENGTH_SHORT).show();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-09 10:27:31

尝试使用以下代码:

代码语言:javascript
运行
复制
 File myDir;
 String fname;
 String id = "123456";
 int count = 0;

String root = Environment.getExternalStorageDirectory().toString();
                myDir = new File(root + "/PhotoApp/" + id);
                myDir.mkdirs();
                count++;

                counter.setText(String.valueOf(count));

                fname = "" + id + "-" + count + ".jpg";

                File file = new File(myDir, fname);
                if (file.exists()) file.delete();

这段代码将创建一个文件夹PhotoApp,在该文件夹中,所有图像都将存储。

您可以在我的GitHub中使用相机获得完整的代码:

链接: https://github.com/abhishekhzb/quick_camera

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66544834

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档