| | |
| | | import java.io.IOException; |
| | | import java.nio.ByteBuffer; |
| | | import java.util.Arrays; |
| | | import java.util.concurrent.ExecutorService; |
| | | import java.util.concurrent.Executors; |
| | | |
| | | import static android.os.Environment.DIRECTORY_DOCUMENTS; |
| | | |
| | |
| | | //主页面的视图 |
| | | private LinearLayout llUpText; |
| | | private TextView tvDevice; |
| | | |
| | | ExecutorService imageExecutor = Executors.newSingleThreadExecutor(); |
| | | |
| | | public Camera2TextureView4(Context context) { |
| | | super(context,null); |
| | |
| | | |
| | | // Image imageCopy = ImageReader.newInstance(videoWidth, videoHeight, ImageFormat.YUV_420_888, 1).acquireLatestImage(); |
| | | byte[] nv12 = new byte[I420size]; |
| | | byte[] nv21 = new byte[I420size]; |
| | | // byte[] nv21 = new byte[I420size]; |
| | | |
| | | YUVUtil.YUVToNV21_NV12(image, nv12, videoWidth, videoHeight, "NV12"); |
| | | long now = image.getTimestamp(); |
| | | image.close(); |
| | | //图片截图 |
| | | SharedPreferences camera2Time = mContext.getSharedPreferences("camera2Time", Context.MODE_PRIVATE); |
| | | long lastTime = camera2Time.getLong("time", 0); |
| | | long secondsGap = (now - lastTime) / 1000000000; |
| | | if (lastTime == 0 || secondsGap > 2) { |
| | | camera2Time.edit().putLong("time", now).apply(); |
| | | //需要将nv12转化为nv21后进行图片存储 |
| | | nv21 = convertNV12toNV21(nv12); |
| | | Bitmap bitmap = BitmapUtil.nv21ToBitmap(nv21, videoWidth, videoHeight); |
| | | BitmapUtil.saveBitmapToFile(bitmap); |
| | | } |
| | | imageExecutor.execute(() -> { |
| | | SharedPreferences camera2Time = mContext.getSharedPreferences("camera2Time", Context.MODE_PRIVATE); |
| | | long lastTime = camera2Time.getLong("time", 0); |
| | | long secondsGap = (now - lastTime) / 1000000000; |
| | | if (lastTime == 0 || secondsGap > 2) { |
| | | camera2Time.edit().putLong("time", now).apply(); |
| | | //需要将nv12转化为nv21后进行图片存储 |
| | | byte[] nv21 = convertNV12toNV21(nv12); |
| | | Bitmap bitmap = BitmapUtil.nv21ToBitmap(nv21, videoWidth, videoHeight); |
| | | BitmapUtil.saveBitmapToFile(bitmap); |
| | | } |
| | | }); |
| | | |
| | | // 提交数据给MediaCodec |
| | | try { |
| | |
| | | |
| | | private void stopPreviewThreadHandler(){ |
| | | //预览的处理线程 |
| | | try { |
| | | /* try { |
| | | previewThread.quitSafely(); |
| | | previewThread.join(); |
| | | } catch (InterruptedException e) { |
| | |
| | | }finally { |
| | | previewThread = null; |
| | | previewThreadHandler = null; |
| | | } |
| | | }*/ |
| | | } |
| | | |
| | | private void initThreadHandlerForRecording() { |