之前的介绍GraphicBuffer传送的内容中只是贴了几句代码,有小伙伴说要看看全部例子,因为代码写的比较乱,以前没有贴完整,现在还是贴出来吧,给需要的同学参考下,
这个程序是一个binder服务端程序,其中myBinder的case 5 是接收客户端发来的GraphicBuffer,再调用图片保存方法,图片保存的方法是仿照screencap源码的,
这里创建了一个名为screenget的binder服务,是一个最基本的binder服务,
注意,测试的时候需要关闭selinux,不然添加服务会出错
#define LOG_TAG "bindertest"
#include <stdio.h>
#include <binder/IInterface.h>
#include <binder/Parcel.h>
#include <binder/IBinder.h>
#include <binder/Binder.h>
#include <binder/ProcessState.h>
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <binder/MemoryBase.h>
#include <binder/MemoryHeapBase.h>
#include <iostream>
#include <iomanip>
#include <unistd.h>
//for ALOGD
#include <log/log.h>
//test socketpair
#include <sys/types.h>
#include <error.h>
#include <errno.h>
#include <sys/socket.h>
#include <pthread.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <linux/fb.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <binder/ProcessState.h>
#include <gui/SurfaceComposerClient.h>
#include <gui/ISurfaceComposer.h>
#include <ui/DisplayInfo.h>
#include <ui/PixelFormat.h>
#include <ui/Region.h>
// TODO: Fix Skia.
//#pragma GCC diagnostic push
//#pragma GCC diagnostic ignored "-Wunused-parameter"
#include <SkImageEncoder.h>
#include <SkData.h>
#include <SkColorSpace.h>
//#pragma GCC diagnostic pop
using namespace android;
static uint32_t DEFAULT_DISPLAY_ID = ISurfaceComposer::eDisplayIdMain;
using namespace std;
//
MemoryBase* mem;
int32_t* pdata = NULL;
int fd = -1;
int socket_pair[2];
void* newThread(void *ptr)
{
for (int i = 0; i < 1000; i++)
{
write(socket_pair[1], "12345", 5);
sleep(1);
cout << "new thread " << endl;
}
return NULL;
}
int icode = 0;
static SkColorType flinger2skia(PixelFormat f)
{
switch (f) {
case PIXEL_FORMAT_RGB_565:
return kRGB_565_SkColorType;
default:
return kN32_SkColorType;
}
}
static sk_sp<SkColorSpace> dataSpaceToColorSpace(android_dataspace d)
{
switch (d) {
case HAL_DATASPACE_V0_SRGB:
return SkColorSpace::MakeSRGB();
case HAL_DATASPACE_DISPLAY_P3:
return SkColorSpace::MakeRGB(
SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kDCIP3_D65_Gamut);
default:
return nullptr;
}
}
int writePNG(char* fileName, char*base, int w, int h, int f, int s)
{
// if(h != 700) return 0;
int fd = open(fileName, O_WRONLY | O_CREAT | O_TRUNC, 0664);
if (fd == -1)
{

这篇博客介绍了如何在Android中创建一个Binder服务端程序,处理客户端传递的GraphicBuffer,并将其保存为PNG图片。程序涉及到关键步骤如内存映射、图片编码及保存。同时,需要注意在测试时需要关闭SELinux以避免服务添加错误。
最低0.47元/天 解锁文章
1674





