android - 在 SurfaceView 中截屏仅在保存时显示黑屏
问题描述
我在 XML 中使用 JavaCameraView 来获取相机:
<org.opencv.android.JavaCameraView
android:id="@+id/activity_surface_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
opencv:camera_id="any"
tools:ignore="MissingConstraints" />
在应用程序的第一页中单击按钮时,它会显示相机。这是我的onCreate()
:
private CameraBridgeViewBase m_CameraView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //세로로 일단 고정
m_CameraView = findViewById(R.id.activity_surface_view);
m_CameraView.setVisibility(SurfaceView.VISIBLE);
m_CameraView.setCvCameraViewListener(this);
m_CameraView.setCameraIndex(m_Camidx);
}
在将图像保存到相册的过程中,我认为问题发生在这里:
public void captureClick(View v) {
SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmss");
Date time = new Date();
String current_time = sdf.format(time);
capture_target = findViewById(R.id.activity_surface_view); // View capture_target;
Request_Capture( capture_target,current_time + "_capture");
Log.e(TAG, "onDestroy : Done onDestroy");
}
public void Request_Capture(View view, String title) {
if (view == null) {
Log.e(TAG, "Request_Capture : view is null");
return;
}
Bitmap bitmap = Bitmap.createBitmap(getBitmapFromView(view));
FileOutputStream fos;
File uploadFolder = getExternalFilesDir("/DCIM/Camera/");
if (!uploadFolder.exists()) {
uploadFolder.mkdir();
}
String Str_Path = "/storage/emulated/0/Android/data/com.example.useopencvwithndk_build/files/DCIM/Camera/";
try {
fos = new FileOutputStream(Str_Path+title+".jpg");
bitmap.compress(Bitmap.CompressFormat.JPEG,80,fos);
} catch (Exception e){
e.printStackTrace();
}
MediaScanner ms = MediaScanner.newInstance(getApplicationContext());
try {
ms.mediaScanning(Str_Path + title + ".jpg");
} catch (Exception e) {
e.printStackTrace();
}
}//End Function
public Bitmap getBitmapFromView(View view){
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
surfaceDestroyed(null);
view.draw(canvas);
surfaceCreated(null);
// onDraw(canvas);
return bitmap;
}
@Override
public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
}
@Override
public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
}
解决方案
推荐阅读
- php - Laradock 在工作区中更改 php-fpm 版本
- javascript - 将新项目添加到状态时反应表附加空行
- javascript - cropper.js - 你如何使用 mouseup 选项来处理cropend 事件?
- android - 使 GradientDrawable 无效
- linux - 如何将cacaview的结果输出到文件中?
- c# - 属性更改的C#事件委托,然后获取值和类名以通过套接字并到达另一端
- c++ - 确定 C++ 应用程序是否作为 UWP 应用程序运行,具有旧版支持
- php - 下拉列表中的值不会传递给 $_POST 中的 php 脚本
- verilog - 模拟中阵列移位器的不同行为
- ios - 无法理解这些 Haxe 函数