view存成bitmap

   
  1. public static String saveView2Bitmap(Context context,View view,String fileName) {
  2. Bitmap bitmap = null;
  3. if(context == null || view == null || TextUtils.isEmpty(fileName)){
  4. return null;
  5. }
  6. view.setDrawingCacheEnabled(true);
  7. // view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
  8. view.buildDrawingCache();
  9. bitmap = view.getDrawingCache();
  10. if(bitmap == null){
  11. Toast.makeText(context,context.getString(R.string.wallpaper_failed_to_crop),Toast.LENGTH_SHORT).show();
  12. return null;
  13. }
  14. String ret = null;
  15. OutputStream fos = null;
  16. try {
  17. Bitmap resizeBitmap = zoomBitmap(bitmap,0.5f,0.5f);
  18. bitmap.recycle();
  19. Uri uri = Uri.fromFile(new File(WallpaperSettingUtils.getTempSaveDir() + fileName));
  20. File file = new File(uri.getPath());
  21. if (!file.exists()) {
  22. try {
  23. file.createNewFile();
  24. } catch (IOException e) {
  25. Toast.makeText(context,context.getString(R.string.wallpaper_failed_to_crop),Toast.LENGTH_SHORT).show();
  26. return null;
  27. }
  28. }
  29. fos = context.getContentResolver().openOutputStream(uri);
  30. // 这里也可以是 Bitmap.CompressFormat.PNG, 可以保持透明背景
  31. resizeBitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
  32. ret = WallpaperSettingUtils.getTempSaveDir() + fileName;
  33. } catch (Exception e) {
  34. Toast.makeText(context,context.getString(R.string.wallpaper_failed_to_crop),Toast.LENGTH_SHORT).show();
  35. }finally {
  36. Util.closeSilently(fos);
  37. view.setDrawingCacheEnabled(false);
  38. }
  39. return ret;
  40. }
  41. /**
  42. * 将图片按指定比例缩放
  43. */
  44. public static Bitmap zoomBitmap(Bitmap bitmap, float scaleWidth, float scaleHeight) {
  45. int width = bitmap.getWidth();
  46. int height = bitmap.getHeight();
  47. Matrix matrix = new Matrix();
  48. // float scaleWidth = ((float) w / width);
  49. // float scaleHeight = ((float) h / height);
  50. matrix.postScale(scaleWidth, scaleHeight);
  51. Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
  52. return newBitmap;
  53. }
  54. public static Bitmap convertViewToBitmap(View view){
  55. view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
  56. view.buildDrawingCache();
  57. Bitmap bitmap = view.getDrawingCache();
  58. return bitmap;
  59. }
  60. //---------------------
  61. public static String saveView2BitmapWithoutZoom(Context context,View view,String fileName) {
  62. Bitmap bitmap = null;
  63. if(context == null || view == null || TextUtils.isEmpty(fileName)){
  64. return null;
  65. }
  66. view.setDrawingCacheEnabled(true);
  67. view.buildDrawingCache();
  68. bitmap = view.getDrawingCache();
  69. if(bitmap == null){
  70. Toast.makeText(context,context.getString(R.string.wallpaper_failed_to_crop),Toast.LENGTH_SHORT).show();
  71. return null;
  72. }
  73. String ret = null;
  74. OutputStream fos = null;
  75. try {
  76. Uri uri = Uri.fromFile(new File(WallpaperSettingUtils.getTempSaveDir() + fileName));
  77. File file = new File(uri.getPath());
  78. if (!file.exists()) {
  79. try {
  80. file.createNewFile();
  81. } catch (IOException e) {
  82. Toast.makeText(context,context.getString(R.string.wallpaper_failed_to_crop),Toast.LENGTH_SHORT).show();
  83. return null;
  84. }
  85. }
  86. fos = context.getContentResolver().openOutputStream(uri);
  87. bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
  88. ret = WallpaperSettingUtils.getTempSaveDir() + fileName;
  89. } catch (Exception e) {
  90. Toast.makeText(context,context.getString(R.string.wallpaper_failed_to_crop),Toast.LENGTH_SHORT).show();
  91. }finally {
  92. Util.closeSilently(fos);
  93. view.destroyDrawingCache();
  94. view.setDrawingCacheEnabled(false);
  95. bitmap.recycle();
  96. }
  97. return ret;
  98. }





### 将 QGraphicsView 转换为 HDC 的实现方法 在 Qt 中,`QGraphicsView` 是用于显示 `QGraphicsScene` 的视图组件。为了将其转换为 Windows 平台上的设备上下文 (HDC),可以采用以下方式: #### 方法概述 可以通过将 `QGraphicsView` 渘到一个临时的图像对象 (`QImage`) 中,再利用该图像创建 GDI 对象并获取其对应的 HDC。 以下是具体实现过程: 1. **渲染 QGraphicsView 到 QImage**: 使用 `QGraphicsView::render()` 函数将视图的内容绘制到一个 `QImage` 上。 2. **从 QImage 获取 HBITMAP 和 HDC**: 创建一个与 `QImage` 兼容的位图句柄,并通过该位图获得设备上下文 (HDC)。 下面是完整的代码示例: ```cpp #include <QApplication> #include <QGraphicsView> #include <QGraphicsScene> #include <QImage> #include <windows.h> HDC convertQGraphicsViewToHDC(QGraphicsView* view) { // Step 1: Create a QImage with the same size as the QGraphicsView. QSize viewSize = view->size(); QImage image(viewSize, QImage::Format_ARGB32); image.fill(Qt::transparent); // Step 2: Render the QGraphicsView content into the QImage. QPainter painter(&image); view->render(&painter); // Render the QGraphicsView to the QImage. // Step 3: Convert QImage to HBITMAP and get its HDC. HBITMAP hBitmap = nullptr; BITMAPINFO bmi = {0}; bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmi.bmiHeader.biWidth = image.width(); bmi.bmiHeader.biHeight = -(LONG)image.height(); // Top-down DIB bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 32; bmi.bmiHeader.biCompression = BI_RGB; void* bits = nullptr; hBitmap = CreateDIBSection(nullptr, &bmi, DIB_RGB_COLORS, &bits, nullptr, 0); if (!hBitmap || !bits) { DeleteObject(hBitmap); return nullptr; } memcpy(bits, image.bits(), static_cast<size_t>(image.byteCount())); // Get the device context of the bitmap. HDC hdc = CreateCompatibleDC(nullptr); SelectObject(hdc, hBitmap); return hdc; // Return the HDC associated with the rendered QGraphicsView. } int main(int argc, char *argv[]) { QApplication app(argc, argv); QGraphicsScene scene; QGraphicsView view(&scene); // Example usage: HDC hdc = convertQGraphicsViewToHDC(&view); if (hdc != nullptr) { // Use the HDC here... DeleteDC(hdc); // Clean up when done. } return app.exec(); } ``` --- #### 关键点说明 - **QGraphicsView::render()**[^1]: 此函数允许将整个视图内容渲染到目标绘图器(如 `QPainter`),从而能够轻松捕获视图中的所有图形项。 - **CreateDIBSection**[^2]: 这是一个 Windows API 函数,用于创建兼容的内设备无关位图 (DIB)。它提供了对像素数据的直接访问权限,因此可以直接复制来自 `QImage` 的数据。 - **SelectObject**[^3]: 将新创建的位图选入指定的 DC 中,以便后续操作可以在该 DC 下完。 - **DeleteDC/Release Resources**[^4]: 确保释放由 `CreateCompatibleDC` 或其他资源管理函数分配的所有资源,以防止内泄漏。 --- #### 注意事项 - 如果需要跨平台支持,则应考虑替代方案,因为 HDC 特定于 Windows 操作系统。 - 在实际应用中可能还需要处理 DPI 缩放问题以及不同分辨率下的适配情况。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值