全景拼接与Android相机源代码

时间:2014-06-03 16:46:28

标签: android panoramas image-stitching

我试图通过拼接一组位图来创建全景图像。 对于这个任务,我使用了android相机的源代码,它有一组全景(马赛克)类。这个代码可以在许多存储库中找到,但无论如何这里是正式的:

https://android.googlesource.com/platform/packages/apps/Camera2/+/android-4.4.3_r1

对于熟悉股票安卓摄像头的用户,全景选项通过平移/移动摄像头来工作,摄像头自动捕获帧以供以后的后期处理。 在我的情况下,我需要实际缝合静止位图,没有相机帧。

所以我创建了一个基于库存相机方法接受位图的功能:

Java_com_android_camera_Mosaic_setSourceImage

接下来推送位图而不是相机帧的方法:

JNIEXPORT jfloatArray JNICALL Java_com_android_camera_Mosaic_setSourceBitmap(JNIEnv* env, jobject thiz, jobject bitmap)
{
    int ret_code = Mosaic::MOSAIC_RET_OK;

    if (frame_number_HR < MAX_FRAMES && frame_number_LR < MAX_FRAMES)
    {
        AndroidBitmapInfo bitmapInfo;
        AndroidBitmap_getInfo(env, bitmap, &bitmapInfo);

        void* bitmapPixels;
        AndroidBitmap_lockPixels(env, bitmap, &bitmapPixels);

        ImageUtils::rgba2yvu(tImage[HR][frame_number_HR], (unsigned char*)bitmapPixels, tWidth[HR], tHeight[HR]);
        AndroidBitmap_unlockPixels(env, bitmap);

        GenerateQuarterResImagePlanar(tImage[HR][frame_number_HR], tWidth[HR], tHeight[HR], tImage[LR][frame_number_LR]);
        decodeYUV444SP(gPreviewImage[LR], tImage[LR][frame_number_LR], gPreviewImageWidth[LR], gPreviewImageHeight[LR]);
        ret_code = AddFrame(LR, frame_number_LR, gTRS);

        if (ret_code == Mosaic::MOSAIC_RET_OK || ret_code == Mosaic::MOSAIC_RET_FEW_INLIERS)
        {
            frame_number_LR++;
            frame_number_HR++;
        }
    }
    else
    {
        gTRS[1] = gTRS[2] = gTRS[3] = gTRS[5] = gTRS[6] = gTRS[7] = 0.0f;
        gTRS[0] = gTRS[4] = gTRS[8] = 1.0f;
    }

    UpdateWarpTransformation(gTRS);

    gTRS[9] = frame_number_HR;
    gTRS[10] = ret_code;

    jfloatArray bytes = env->NewFloatArray(11);

    if (bytes != 0)
    {
        env->SetFloatArrayRegion(bytes, 0, 11, (jfloat*)gTRS);
    }

    return bytes;
}

为了生成全景图,我还做了下一个工作流程,也基于android源代码:

private static Bitmap loadBitmap(final String filename)
{
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = BitmapFactory.decodeFile(filename, options);
    return bitmap;
}

public int createMosaicFromBitmaps(final boolean highRes, final List<String> images)
{
    for (String file : images)
    {
        final Bitmap bitmap = MosaicFrameProcessor.loadBitmap(file);
        this._mosaicer.setSourceBitmap(bitmap);
        bitmap.recycle();
    }

    return this._mosaicer.createMosaic(true);
}

public void generatePanorama()
{
    final MosaicFrameProcessor mosaicProcessor = new MosaicFrameProcessor(1280, 720);

    final List<String> imagesList = new ArrayList<String>();
    imagesList.add("storage/extSdCard/Test2/A1.jpg");
    imagesList.add("storage/extSdCard/Test2/A2.jpg");
    imagesList.add("storage/extSdCard/Test2/A3.jpg");

    final int mosaicReturnCode = createMosaicFromBitmaps(false, imagesList);

    if (mosaicReturnCode == Mosaic.MOSAIC_RET_CANCELLED || mosaicReturnCode == Mosaic.MOSAIC_RET_ERROR)
    {
        return;
    }

    int[] imageData = mosaicProcessor.getFinalMosaic();

    if (imageData == null)
    {
        Log.e(PanoramaActivity.TAG, "getFinalMosaic() returned null.");
        return;
    }

    final int height = imageData[imageData.length - 1];
    final int width = imageData[imageData.length - 2];

    final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    System.arraycopy(imageData, 0, imageData, 0, imageData.length - 2);
    bitmap.setPixels(imageData, 0, width, 0, 0, width, height);

    final ImageView image = (ImageView)this.findViewById(com.example.customcamera.R.id.image);
    image.setImageBitmap(bitmap);
    image.invalidate();
}

我面临的问题是,通过迹线我可以看到测试位图被接受,它们通过验证,并且全景被缝合。但返回的最终输出图像全部混淆了。结果是包含3个测试图像的部分/部分的单个图像帧,以及不正确的对齐和混合。

0 个答案:

没有答案