How can i Convert 4 channel image to 3 channel image?

how can i Convert 4 channel image to 3 channel image?
i was using this code long time ago now i’m getting a error.

    photo.setFromPixels(camera.getPixelsRef());
    nChannels = photo.getPixelsRef().getNumChannels();
    photoCvPix = photoCV.getPixels();
    photoPix = photo.getPixels();
    for (int i = 0; i < capW * capH; i++){
    photoCvPix[ i * 3 + 0] = photoPix[i*nChannels + 0];
    photoCvPix[ i * 3 + 1] = photoPix[i*nChannels + 1];
    photoCvPix[ i * 3 + 2] = photoPix[i*nChannels + 2];
        }
    
    photoCV.flagImageChanged();

Does this work?

if (pixels.getNumChannels() > 3) {
     mPixels = pixels;
     mPixels.setNumChannels(3);
}

yes thanks