如何提高 ARCore 中的预览相机质量

how to increase preview camera quality in ARCore

我在 android 项目中使用 ARCore。它有一个 activity, MainActivity.java 和一个 CustomArFragment.java.

问题是相机质量很差。如何提高相机预览分辨率。将来,我需要使用相机识别文本,并且质量很低,很难识别。我无法在 google 上找到解决方案。尝试了很多东西但没有成功。任何人请告诉如何提高相机质量。我是 AR 和 android 领域的新手。

谢谢:)

MainActivity.java :

public class MainActivity extends AppCompatActivity {

private CustomArFragment arFragment;
private TextView textView;
private AugmentedImageDatabase aid;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

arFragment = (CustomArFragment) getSupportFragmentManager().findFragmentById(R.id.arFragment);
textView = findViewById(R.id.textView);

arFragment.getArSceneView().getScene().addOnUpdateListener(this::onUpdate);

findViewById(R.id.registeredBtn).setOnClickListener(v -> {
    if(ActivityCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
        return;
    }
    registeredImage();
});


}


private void registeredImage() {
File file = new File(getExternalFilesDir(null) + "/db.imgdb");
try {

    FileOutputStream outputStream = new FileOutputStream(file);
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.earth);
    aid.addImage("earth",bitmap);
    aid.serialize(outputStream);
    outputStream.close();
    Toast.makeText(this, "image Registered", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
    e.printStackTrace();
 }
}

private void onUpdate(FrameTime frameTime) {
frame = arFragment.getArSceneView().getArFrame();
Collection<AugmentedImage> images = frame.getUpdatedTrackables(AugmentedImage.class);
for(AugmentedImage image : images){
    if(image.getTrackingMethod() == AugmentedImage.TrackingMethod.FULL_TRACKING){
        if(image.getName().equals("lion")){
            textView.setText("LION is visible");
        }
        else if(image.getName().equals("download")){
            textView.setText("download is visible");
        }
        else{
            textView.setText("Nothing is visible till now");
        }
        Log.d("Value of textView : "," " + textView.getText());
    }
    Log.d("Value of textView1 : "," " + textView.getText());
  }
 }

 public void loadDB(Session session, Config config){
//InputStream dbStream = getResources().openRawResource(R.raw.imagedb);
try {
    File file = new File(getExternalFilesDir(null) + "/db.imgdb");
    FileInputStream dbStream = new FileInputStream(file);
    aid = AugmentedImageDatabase.deserialize(session, dbStream);
    config.setAugmentedImageDatabase(aid);
    session.configure(config);
    Log.d("TotalImages"," :  " + aid.getNumImages());

}catch (IOException e){
    e.printStackTrace();
}
}

CustomArFragment.java

public class CustomArFragment extends ArFragment {
@Override
protected Config getSessionConfiguration(Session session){
    getPlaneDiscoveryController().setInstructionView(null);
    Config config = new Config(session);
    config.setUpdateMode(Config.UpdateMode.LATEST_CAMERA_IMAGE);
    MainActivity activity = (MainActivity) getActivity();
    activity.loadDB(session,config);
    this.getArSceneView().setupSession(session);
    return config;
}
}

我在 this other question. But answering yours, check what CameraConfig is used by default (use getCameraConfig() on the Session). Then you can get the one that is of your interest (high GPU texture size) through getSupportedCameraConfigs() and configure the Session with it through setCameraConfig 中添加了一些关于类似主题的信息。

再次阅读您的问题,如果您将来需要识别图像上的某些文本,您可能希望获得更高的 CPU 图像而不是 GPU 纹理大小。您仍然可以使用 CameraConfig 检查并选择您最喜欢的,但请记住,更高的 CPU 图像会大大降低性能。你可能想看看我之前提到的问题的答案。

编辑:使用此代码片段检查正在使用的 CameraConfig 并根据您的需要进行调整:

Session session = new Session(requireActivity());

// ...

Size selectedSize = new Size(0, 0);
CameraConfig selectedCameraConfig = null;

CameraConfigFilter filter = new CameraConfigFilter(session);
List<CameraConfig> cameraConfigsList = session.getSupportedCameraConfigs(filter);
for (CameraConfig currentCameraConfig : cameraConfigsList) {
    Size cpuImageSize = currentCameraConfig.getImageSize();
    Size gpuTextureSize = currentCameraConfig.getTextureSize();
    Log.d("TAG", "CurrentCameraConfig CPU image size:" + cpuImageSize + " GPU texture size:" + gpuTextureSize);

    // Adapt this check to your needs
    if (gpuTextureSize.getWidth() > selectedSize.getWidth()) {
        selectedSize = gpuTextureSize;
        selectedCameraConfig = currentCameraConfig;
    }
}

Log.d("TAG", "Selected CameraConfig CPU image size:" + selectedCameraConfig.getImageSize() + " GPU texture size:" + selectedCameraConfig.getTextureSize());
session.setCameraConfig(selectedCameraConfig);

// ...

// Don't forget to configure the session afterwards
session.configure(config);