pixpark/gpupixel

安卓SDK集成问题

qwonderkid opened this issue · 2 comments

尝试把gpupixel接入一个安卓项目。首先使用example中的GPUPixelSourceCamera类可以正常工作。想拿到输出图像的数据做进一步处理。之前在windows平台集成sdk使用sourceImage输入处理画面并用captureAProcessedFrameData()方法也可以正常工作。在安卓中调用几帧后崩溃,附调用代码和堆栈图。请问是否可以指点一下

package com.pixpark.GPUPixelApp;

import static android.widget.Toast.LENGTH_LONG;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Camera;
import android.os.Bundle;
import android.util.Log;
import android.util.Size;
import android.view.SurfaceHolder;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;

import com.pixpark.GPUPixelApp.camera.CameraXHelper;
import com.pixpark.GPUPixelApp.camera.CustomLifecycleOwner;
import com.pixpark.GPUPixelApp.databinding.ActivityMainBinding;
import com.pixpark.gpupixel.GPUPixel;
import com.pixpark.gpupixel.GPUPixelSource;
import com.pixpark.gpupixel.GPUPixelSourceImage;
import com.pixpark.gpupixel.GPUPixelSourceRawInput;
import com.pixpark.gpupixel.filter.BeautyFaceFilter;
import com.pixpark.gpupixel.filter.FaceReshapeFilter;
import com.pixpark.gpupixel.GPUPixelSourceCamera;
import com.pixpark.gpupixel.GPUPixelView;
import com.pixpark.gpupixel.filter.LipstickFilter;
import com.pixpark.gpupixel.filter.BlusherFilter;

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
private static final int CAMERA_PERMISSION_REQUEST_CODE = 200;
private static final String TAG = "GPUPixelDemo";

private GPUPixelSourceCamera sourceCamera;
private GPUPixelView surfaceView;
private BeautyFaceFilter beautyFaceFilter;
private FaceReshapeFilter faceReshapFilter;
private LipstickFilter lipstickFilter;
private  BlusherFilter blusherFilter;
private GPUPixelSourceImage sourceImage;
private SeekBar smooth_seekbar;
private SeekBar whiteness_seekbar;
private SeekBar face_reshap_seekbar;
private SeekBar big_eye_seekbar;
private SeekBar lipstick_seekbar;
private SeekBar blusher_seekbar;
private  SeekBar foreheadwid_seekbar;

private ActivityMainBinding binding;

ImageView imageview;
private CameraXHelper cameraXHelper;
private CustomLifecycleOwner customLifecycleOwner;

private  byte[] pixel;

GPUPixelSource.ProcessedFrameDataCallback callback = new GPUPixelSource.ProcessedFrameDataCallback() {
    @Override
    public void onResult(Bitmap result) {
        int pixelCount = result.getWidth() * result.getHeight();
        Log.d("BitmapLength", "Bitmap pixel data length: " + pixelCount);

    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());

    // get log path
    String path = getExternalFilesDir("gpupixel").getAbsolutePath();
    Log.i(TAG, path);

    GPUPixel.setContext(this);
    // 保持屏幕常亮
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    // preview
    surfaceView = binding.surfaceView;
    surfaceView.setMirror(true);

    blusher_seekbar = binding.bluserSeekbar;

    imageview = binding.imageview;


    cameraXHelper=new CameraXHelper(this);


    customLifecycleOwner=new CustomLifecycleOwner();

    cameraXHelper.addListener(new CameraXHelper.OnFrameListener() {
        @Override
        public void onFrame(byte[] data, int width, int height, boolean isBack) {
            int[] rbga=new int[width*height];
            GPUPixel.nativeYUVtoRBGA(data,width,height,rbga);

            Bitmap bitmap=rgbaToBitmap(rbga,height,width);
            imageview.setImageBitmap(bitmap);
            if(sourceImage != null && faceReshapFilter != null) {
                sourceImage.setImage(bitmap);
                sourceImage.proceed();
                GPUPixel.getInstance().requestRender();
                sourceImage.captureAProcessedFrameData(faceReshapFilter, width,height,callback);

            }
        }
    });


    blusher_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            blusherFilter.setBlendLevel(progress/5.0f);

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });


    foreheadwid_seekbar = binding.foreheadwidSeekbar;

    foreheadwid_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            faceReshapFilter.setForeheadWidthLevel(progress / 100.0f);


        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });


    smooth_seekbar = binding.smoothSeekbar;
    smooth_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            faceReshapFilter.setFaceLengthLevel(-progress / 100.0f);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

    whiteness_seekbar = binding.whitenessSeekbar;
    whiteness_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            beautyFaceFilter.setWhiteLevel(progress / 10.0f);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });


    face_reshap_seekbar = binding.thinfaceSeekbar;
    face_reshap_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            faceReshapFilter.setThinLevel(progress / 200.0f);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

    big_eye_seekbar = binding.bigeyeSeekbar;
    big_eye_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            faceReshapFilter.setThinCheekLevel(progress / 100.0f);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

    lipstick_seekbar = binding.lipstickSeekbar;
    lipstick_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            faceReshapFilter.setJawWidthLevel(progress / 100.0f);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

    //
    this.checkCameraPermission();

}
public static Bitmap rgbaToBitmap(int[] rgba, int w, int h){
    return Bitmap.createBitmap(rgba, w, h, Bitmap.Config.ARGB_8888);
}

public void startCameraFilter() {

    Size size=new Size(640,480);
    cameraXHelper.startCamera(customLifecycleOwner,size);

    // 美颜滤镜
    beautyFaceFilter = new BeautyFaceFilter();
    faceReshapFilter = new FaceReshapeFilter();
    lipstickFilter = new LipstickFilter();
    blusherFilter = new BlusherFilter();

    // camera
    //sourceCamera = new GPUPixelSourceCamera(this.getApplicationContext());
    sourceImage = new GPUPixelSourceImage(null);
    //filter
    sourceImage.addTarget(lipstickFilter);
    lipstickFilter.addTarget(faceReshapFilter);
    faceReshapFilter.addTarget(beautyFaceFilter);
    beautyFaceFilter.addTarget(blusherFilter);
    blusherFilter.addTarget(surfaceView);

    sourceImage.setLandmarkCallbck(new GPUPixel.GPUPixelLandmarkCallback() {
        @Override
        public void onFaceLandmark(float[] landmarks) {
            faceReshapFilter.setFaceLandmark(landmarks);
            lipstickFilter.setFaceLandmark(landmarks);
            blusherFilter.setFaceLandmark(landmarks);
        }
    });


    // set default value
    // beautyFaceFilter.setSmoothLevel(0.5f);
    // beautyFaceFilter.setWhiteLevel(0.4f);
}


public void checkCameraPermission() {
    // 检查相机权限是否已授予
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        // 如果未授予相机权限,向用户请求权限
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
    } else {
        startCameraFilter();
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == CAMERA_PERMISSION_REQUEST_CODE) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            startCameraFilter();
        } else {
            Toast.makeText(this, "No Camera permission!", LENGTH_LONG);
        }
    }
}


public void surfaceCreated(SurfaceHolder holder) {
    sourceCamera.setPreviewHolder(holder);
}

@Override
public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(@NonNull SurfaceHolder holder) {

}

}

2024-04-16 10:13:44.454 18882-18908 libc com.pixpark.gpupixeldemo A Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x100000010 in tid 18908 (GLThread 2137), pid 18882 (rk.gpupixeldemo)
2024-04-16 10:13:44.614 18956-18956 DEBUG pid-18956 A pid: 18882, tid: 18908, name: GLThread 2137 >>> com.pixpark.gpupixeldemo <<<
2024-04-16 10:13:44.693 18956-18956 DEBUG pid-18956 A #00 pc 00000000001a3248 /data/app/~~MRiQLZE6DXOm3GNMT1FPdw==/com.pixpark.gpupixeldemo-9Fb4S55b7JRFQwvnArgEFQ==/lib/arm64/libgpupixel.so (gpupixel::Source::updateTargets(long)+212) (BuildId: 717c77cce68743ed053f91c46e7073768105833c)
2024-04-16 10:13:44.693 18956-18956 DEBUG pid-18956 A #1 pc 00000000001a3154 /data/app/~~MRiQLZE6DXOm3GNMT1FPdw==/com.pixpark.gpupixeldemo-9Fb4S55b7JRFQwvnArgEFQ==/lib/arm64/libgpupixel.so (gpupixel::Source::proceed(bool, long)+72) (BuildId: 717c77cce68743ed053f91c46e7073768105833c)
2024-04-16 10:13:44.693 18956-18956 DEBUG pid-18956 A #2 pc 000000000010bf4c /data/app/~~MRiQLZE6DXOm3GNMT1FPdw==/com.pixpark.gpupixeldemo-9Fb4S55b7JRFQwvnArgEFQ==/lib/arm64/libgpupixel.so (gpupixel::FaceMakeupFilter::proceed(bool, long)+7816) (BuildId: 717c77cce68743ed053f91c46e7073768105833c)
2024-04-16 10:13:44.693 18956-18956 DEBUG pid-18956 A #3 pc 000000000012cfc0 /data/app/~~MRiQLZE6DXOm3GNMT1FPdw==/com.pixpark.gpupixeldemo-9Fb4S55b7JRFQwvnArgEFQ==/lib/arm64/libgpupixel.so (gpupixel::Filter::update(long)+1580) (BuildId: 717c77cce68743ed053f91c46e7073768105833c)
2024-04-16 10:13:44.693 18956-18956 DEBUG pid-18956 A #4 pc 000000000012d0a0 /data/app/~~MRiQLZE6DXOm3GNMT1FPdw==/com.pixpark.gpupixeldemo-9Fb4S55b7JRFQwvnArgEFQ==/lib/arm64/libgpupixel.so (non-virtual thunk to gpupixel::Filter::update(long)+36) (BuildId: 717c77cce68743ed053f91c46e7073768105833c)
2024-04-16 10:13:44.693 18956-18956 DEBUG pid-18956 A #5 pc 00000000001a32a4 /data/app/~~MRiQLZE6DXOm3GNMT1FPdw==/com.pixpark.gpupixeldemo-9Fb4S55b7JRFQwvnArgEFQ==/lib/arm64/libgpupixel.so (gpupixel::Source::updateTargets(long)+304) (BuildId: 717c77cce68743ed053f91c46e7073768105833c)
2024-04-16 10:13:44.693 18956-18956 DEBUG pid-18956 A #6 pc 00000000001a3154 /data/app/~~MRiQLZE6DXOm3GNMT1FPdw==/com.pixpark.gpupixeldemo-9Fb4S55b7JRFQwvnArgEFQ==/lib/arm64/libgpupixel.so (gpupixel::Source::proceed(bool, long)+72) (BuildId: 717c77cce68743ed053f91c46e7073768105833c)
2024-04-16 10:13:44.693 18956-18956 DEBUG pid-18956 A #7 pc 00000000002588e4 /data/app/~~MRiQLZE6DXOm3GNMT1FPdw==/com.pixpark.gpupixeldemo-9Fb4S55b7JRFQwvnArgEFQ==/lib/arm64/libgpupixel.so (Java_com_pixpark_gpupixel_GPUPixel_nativeSourceProceed+96) (BuildId: 717c77cce68743ed053f91c46e7073768105833c)
2024-04-16 10:13:44.693 18956-18956 DEBUG pid-18956 A #15 pc 00000000000023a8 [anon:dalvik-classes3.dex extracted in memory from /data/app/~~MRiQLZE6DXOm3GNMT1FPdw==/com.pixpark.gpupixeldemo-9Fb4S55b7JRFQwvnArgEFQ==/base.apk!classes3.dex] (com.pixpark.gpupixel.GPUPixelSource$4.run+32)
2024-04-16 10:13:44.693 18956-18956 DEBUG pid-18956 A #18 pc 000000000000213e [anon:dalvik-classes3.dex extracted in memory from /data/app/~~MRiQLZE6DXOm3GNMT1FPdw==/com.pixpark.gpupixeldemo-9Fb4S55b7JRFQwvnArgEFQ==/base.apk!classes3.dex] (com.pixpark.gpupixel.GPUPixelRenderer.runAll+26)
2024-04-16 10:13:44.693 18956-18956 DEBUG pid-18956 A #21 pc 00000000000020ca [anon:dalvik-classes3.dex extracted in memory from /data/app/~~MRiQLZE6DXOm3GNMT1FPdw==/com.pixpark.gpupixeldemo-9Fb4S55b7JRFQwvnArgEFQ==/base.apk!classes3.dex] (com.pixpark.gpupixel.GPUPixelRenderer.onDrawFrame+14)