From 5c25dd0a2f0cf40a1f1827863221a75f0066c4a9 Mon Sep 17 00:00:00 2001
From: whycxzp <glperry@163.com>
Date: 星期六, 27 七月 2024 10:00:07 +0800
Subject: [PATCH] 更新

---
 app/src/main/java/com/whyc/widget/Camera2TextureView2.java |   33 +++++++++++++++++++++++----------
 1 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/app/src/main/java/com/whyc/widget/Camera2TextureView2.java b/app/src/main/java/com/whyc/widget/Camera2TextureView2.java
index dd91dc8..c3976f3 100644
--- a/app/src/main/java/com/whyc/widget/Camera2TextureView2.java
+++ b/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("杩欐槸娴嬭瘯鐨勫姩鎬佽祴鍊�");
             // 淇濆瓨鍥剧墖鍒扮浉鍐�-杩欑鏂瑰紡鏄疛EPG鏍煎紡
-            //            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();
         }
     }
 

--
Gitblit v1.9.1