android - 相机捕捉自定义视图Android
问题描述
我正在一起学习 Surfaceview 和 Camera。
我似乎无法捕获我的 CustomSurfaceView,下面是我的 OnCreate 的代码以及我如何执行 CustomSurfaceView 的捕获。
以下是我研究过的链接: https ://inneka.com/programming/android/how-does-androids-setdrawingcacheenabled-work/
https://arpitonline.com/2012/07/17/capturing-bitmaps-of-views-in-android/
这是我理解的需要;我试过了,但还是不行
//customSurfaceView.setVisibility(View.VISIBLE)
//customSurfaceView.setDrawingCacheEnabled(true)
//customSurfaceView.buildDrawingCache()
private CustomSurfaceView customSurfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.iv_screenshot);
surfaceView = findViewById(R.id.surfaceView);
if (surfaceView != null) {
boolean result = checkPermission();
if (result) {
setupSurfaceHolder();
}
}
if (canvasLayout == null) {
canvasLayout = (LinearLayout) findViewById(R.id.customViewLayout);
}
// Create custom surfaceview object.
customSurfaceView = new CustomSurfaceView(getApplicationContext());
// customSurfaceView.set
// Set this as the onTouchListener to process custom surfaceview ontouch event.
customSurfaceView.setOnTouchListener(this);
// Add the custom surfaceview object to the layout.
canvasLayout.addView(customSurfaceView);
}
private void saveSecondSurfaceVice() {
View content = customSurfaceView;
// View content = getWindow().getDecorView().getRootView();
// customSurfaceView.setVisibility(View.VISIBLE);
content.setDrawingCacheEnabled(true);
// content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
content.buildDrawingCache(true);
Bitmap bitmap = content.getDrawingCache().copy(Bitmap.Config.ARGB_8888, false);
String fileName = "CanvasSurface" + System.currentTimeMillis() + ".jpg";
Log.i("TAGS", "content.getDrawingCache() = " + content.getDrawingCache() + " | bitmap = " + bitmap);
File file = new File(Environment.getExternalStorageDirectory(), fileName);
FileOutputStream ostream;
content.setDrawingCacheEnabled(false);
// Bitmap bitmap1 = bitmap.copy(Bitmap.Config.ARGB_8888, false);
// imageView.setImageBitmap(bitmap1);
// // check empty bitmap testing only
// Bitmap emptyBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
//
// if (bitmap.sameAs(emptyBitmap)) {
// imageView.setVisibility(View.GONE);
// Log.i("TAGS", "bitmap is empty");
// } else {
// imageView.setVisibility(View.VISIBLE);
// imageView.setImageBitmap(bitmap);
// Log.i("TAGS", "bitmap is not empty");
// }
// content.destroyDrawingCache();
// imageView.setImageResource(R.drawable.ic_launcher_background);
try {
file.createNewFile();
ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, ostream);
ostream.flush();
ostream.close();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
}
}
非常感谢指导:)
解决方案
如果您的最低 API 级别至少为 24,则可以使用 PixelHolder。这是根据Android 文档推荐的屏幕截图方法。实际上,从 API 级别 28 开始不推荐使用 buildDrawingCache 方法。
例如:
SurfaceHolder holder = customSurfaceView.getHolder();
Bitmap bitmap = Bitmap.createBitmap(100,100,Bitmap.Config.ARGB_8888);
PixelCopy.request(holder.getSurface(), bitmap, (copyResult -> {
//success/failure handler
}), new Handler());
推荐阅读
- node.js - Prevent node.js from reading node_modules besides immediate folder
- dart - 需要处理的 Dart Analyzer 源
- matrix - How to display 4 quadrants on Cartesian graph? mathematica
- c# - EF Core expression being evaluated locally... why?
- c# - Unity Class Instantiate C#
- java - I have to write a Java program that displays how many lines would pass through the origin (0,0)
- python - 如何创建表条目的实例但未在 ponyorm 中添加?
- node.js - 如何使用 AWS 开发工具包以编程方式扩展 ECS 集群
- amazon-web-services - 一种将数据从一个 S3 复制到另一个 S3 存储桶的自动方法
- c# - Listview getItemAt 返回 null