gpt4 book ai didi

android - 相机捕获自定义 View Android

转载 作者:行者123 更新时间:2023-11-30 04:58:52 25 4
gpt4 key购买 nike

我正在一起学习 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 documentation,这是推荐的屏幕截图方法。 .实际上,从 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());

关于android - 相机捕获自定义 View Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58600926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com