前言
最近做车机camera 倒车影像问题,需要通过c++调用camera,并显示在android ui界面之上。
最终效果图
代码实现
Android.bp
cc_binary {
name: "stest",
vendor: true,
srcs: ["main.cpp"],
shared_libs: [
"libcutils",
"libbase",
"liblog",
"libgui_vendor",
"libutils",
"libhidlbase",
"libhidltransport",
"libhwbinder",
"libbinder",
"libui",
//"vendor.qti.hardware.meig.camera@1.0"
],
include_dirs: ["frameworks/native/include"],
cflags: [
"-Wall",
"-Wno-unused-parameter",
"-Werror",
"-Wno-multichar",
],
}
main.cpp
#define LOG_TAG "stest"
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <cutils/log.h>
#include <fcntl.h>
#include <android-base/logging.h>
#include <android-base/properties.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/epoll.h>
#include <cutils/uevent.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
//#include <time.h>
//#include <dlfcn.h>
#include <gui/ISurfaceComposer.h>
#include <gui/Surface.h>
#include <gui/SurfaceComposerClient.h>
#include <android/native_window.h>
#include <ui/DisplayInfo.h>
#include <ui/Rect.h>
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <cutils/memory.h>
using namespace android;
int main(int argc, char *argv[]) {
ALOGE("wjc main start");
int i=0;
int x=0;
int y=0;
android::ProcessState::self()->startThreadPool();
sp<SurfaceComposerClient> client = new SurfaceComposerClient();
ALOGE("wjc main create Surface");
sp<SurfaceControl> surfaceControl = client->createSurface(String8("resize"),
600, 600, PIXEL_FORMAT_RGB_565, 0);
ALOGE("wjc main create Surface end");
sp<Surface> surface = surfaceControl->getSurface();
ALOGE("wjc main Transaction start");
android::SurfaceComposerClient::Transaction{}
.setSize(surfaceControl, 400, 400)
.setLayer(surfaceControl, 0x40000000)
.setPosition(surfaceControl, 400,80)
//.setAlpha(surfaceControl, 0.5f)
.setCornerRadius(surfaceControl, 10.0f)
.show(surfaceControl)
.apply();
ALOGE("wjc main Transaction end");
ANativeWindow_Buffer outBuffer;
sp<ANativeWindow> anw = surfaceControl->getSurface();
while(true){
surface->lock(&outBuffer, NULL);
ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format);
android::SurfaceComposerClient::Transaction{}
.setSize(surfaceControl, 400, 400)
.setLayer(surfaceControl, 0x40000000)
.setPosition(surfaceControl, x%1920,y%400)
.setAlpha(surfaceControl, 0.5f)
.show(surfaceControl)
.apply();
if(i%3 == 0){
android_memset16((uint16_t*)outBuffer.bits, 0xF800, bpr*outBuffer.height);
} else if (i%3 == 1){
android_memset16((uint16_t*)outBuffer.bits, 0x07E0, bpr*outBuffer.height);
} else if (i%3 == 2){
android_memset16((uint16_t*)outBuffer.bits, 0x001F , bpr*outBuffer.height);
}
surface->unlockAndPost();
//sleep(1);
usleep(10);
i++;
x++;
y++;
}
ALOGE("wjc main end");
return 0;
}