whycxzp
2024-07-27 5c25dd0a2f0cf40a1f1827863221a75f0066c4a9
更新
1个文件已修改
33 ■■■■■ 已修改文件
app/src/main/java/com/whyc/widget/Camera2TextureView2.java 33 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/whyc/widget/Camera2TextureView2.java
@@ -33,7 +33,9 @@
import com.whyc.util.BitmapUtil;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
@@ -194,7 +196,7 @@
            mainCameraDevice = cameraDevice;
            //拍照的接收器
            mImageReader = ImageReader.newInstance(outputSizes.getWidth(), outputSizes.getHeight(), ImageFormat.YUV_420_888, 2);
            mImageReader = ImageReader.newInstance(outputSizes.getWidth(), outputSizes.getHeight(), ImageFormat.JPEG, 2);
            //            mImageReader = ImageReader.newInstance(outputSizes.getWidth(), outputSizes.getHeight(), ImageFormat.JPEG, 4);
            //创建主摄像头会话并预览
            createPreviewSession();
@@ -357,16 +359,16 @@
            //            llUpText.setVisibility(View.VISIBLE);
            //            tvDevice.setText("这是测试的动态赋值");
            // 保存图片到相册-这种方式是JEPG格式
            //            saveImageToGallery(image);
            saveImageToGallery(image);
            //YUV420
            //            int I420size = image.getWidth()*image.getHeight()*3/2;
            //            byte[] nv21 = new byte[I420size];
            //            YUVUtil.YUVToNV21_NV12(image,nv21,outputSizes.getWidth(),outputSizes.getHeight(),"NV21");
            List<byte[]> planetBytes = getPlanetBytes(image);
            /*List<byte[]> planetBytes = getPlanetBytes(image);
            byte[] nv21 = byteMergerAll(planetBytes.get(0), planetBytes.get(1));
            Bitmap bitmap = BitmapUtil.nv21ToBitmap(nv21, outputSizes.getWidth(), outputSizes.getHeight());
            BitmapUtil.saveBitmapToFile(bitmap);
            BitmapUtil.saveBitmapToFile(bitmap);*/
        }catch (Exception e){
            e.printStackTrace();
        }finally {
@@ -420,12 +422,21 @@
        byte[] data = new byte[buffer.remaining()];
        buffer.get(data);
        // 创建文件输出流
        try {
//        FileOutputStream fos = null;
        /*try {
            fos = new FileOutputStream(filePath);
            fos.write(data);*/
        try (
            FileOutputStream fos = new FileOutputStream(filePath);
            fos.write(data);
            fos.close();
            image.close();
            // 通知图库更新
            BufferedOutputStream bos = new BufferedOutputStream(fos)){
            bos.write(data);
        } catch (FileNotFoundException fileNotFoundException) {
            fileNotFoundException.printStackTrace();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
        try {
        // 通知图库更新
            MediaScannerConnection.scanFile(mContext, new String[]{filePath}, null, null);
            // 在某些设备上,可能需要发送广播通知才能使图片立即出现在相册中
@@ -433,10 +444,12 @@
            // 显示保存成功的提示
            //            Toast.makeText(this, "图片保存成功", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
        } catch (Exception e) {
            e.printStackTrace();
            // 显示保存失败的提示
            //            Toast.makeText(this, "图片保存失败", Toast.LENGTH_SHORT).show();
        }finally {
            image.close();
        }
    }