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

huangapple go评论72阅读模式
英文:

how to increase preview camera quality in ARCore

问题

我在我的Android项目中使用了ARCore。它有一个活动,MainActivity.java和一个CustomArFragment.java。

问题是相机的质量非常低。如何增加相机预览分辨率。将来我需要使用相机识别文本,但由于质量太低,很难识别。我在谷歌上找不到解决方案。尝试了很多方法,但没有成功。请问有人可以告诉如何提高相机质量。我在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){
        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;
    }
}
英文:

I'm using ARCore in my android project. It has one activity, MainActivity.java and one CustomArFragment.java.

The problem is that the camera quality is very low. how can I increase the camera preview resolution. In future , I need the recognize the text using camera and having such low quality, its difficult to recognize. I am not able to find the solution on google. Tried many things but got no success. Anyone please tell how to increase camera quality. I'm totally new in AR and android field.

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

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 -&gt; {
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) + &quot;/db.imgdb&quot;);
try {
FileOutputStream outputStream = new FileOutputStream(file);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.earth);
aid.addImage(&quot;earth&quot;,bitmap);
aid.serialize(outputStream);
outputStream.close();
Toast.makeText(this, &quot;image Registered&quot;, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
private void onUpdate(FrameTime frameTime) {
frame = arFragment.getArSceneView().getArFrame();
Collection&lt;AugmentedImage&gt; images = frame.getUpdatedTrackables(AugmentedImage.class);
for(AugmentedImage image : images){
if(image.getTrackingMethod() == AugmentedImage.TrackingMethod.FULL_TRACKING){
if(image.getName().equals(&quot;lion&quot;)){
textView.setText(&quot;LION is visible&quot;);
}
else if(image.getName().equals(&quot;download&quot;)){
textView.setText(&quot;download is visible&quot;);
}
else{
textView.setText(&quot;Nothing is visible till now&quot;);
}
Log.d(&quot;Value of textView : &quot;,&quot; &quot; + textView.getText());
}
Log.d(&quot;Value of textView1 : &quot;,&quot; &quot; + textView.getText());
}
}
public void loadDB(Session session, Config config){
//InputStream dbStream = getResources().openRawResource(R.raw.imagedb);
try {
File file = new File(getExternalFilesDir(null) + &quot;/db.imgdb&quot;);
FileInputStream dbStream = new FileInputStream(file);
aid = AugmentedImageDatabase.deserialize(session, dbStream);
config.setAugmentedImageDatabase(aid);
session.configure(config);
Log.d(&quot;TotalImages&quot;,&quot; :  &quot; + 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;
}
}

答案1

得分: 4

我在这个其他问题中添加了一些关于类似主题的信息。但回答您的问题,请检查默认情况下使用的CameraConfig(在Session中使用getCameraConfig())。然后,您可以通过getSupportedCameraConfigs()获取您感兴趣的配置(高GPU纹理大小),并通过setCameraConfig将Session配置为使用它。

重新阅读您的问题,如果将来需要识别图像上的一些文本,您可能希望获取更高的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);

    // 根据您的需求进行适应此检查
    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);

// ...

// 不要忘记在此之后配置会话
session.configure(config);
英文:

I added some info regarding a similar topic in 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.

Reading your question again, if you need in the future to recognize some text on the image, you might want to get a higher CPU image instead of GPU texture size. You can still use CameraConfig to check that and choose your favorite, but keep in mind that higher CPU images decreases the performance quite a bit. You might want to check my answer in the question I mentioned earlier.

EDIT: Use this code snippet to check the CameraConfig being used and adapt it to your needs:

Session session = new Session(requireActivity());
// ...
Size selectedSize = new Size(0, 0);
CameraConfig selectedCameraConfig = null;
CameraConfigFilter filter = new CameraConfigFilter(session);
List&lt;CameraConfig&gt; cameraConfigsList = session.getSupportedCameraConfigs(filter);
for (CameraConfig currentCameraConfig : cameraConfigsList) {
Size cpuImageSize = currentCameraConfig.getImageSize();
Size gpuTextureSize = currentCameraConfig.getTextureSize();
Log.d(&quot;TAG&quot;, &quot;CurrentCameraConfig CPU image size:&quot; + cpuImageSize + &quot; GPU texture size:&quot; + gpuTextureSize);
// Adapt this check to your needs
if (gpuTextureSize.getWidth() &gt; selectedSize.getWidth()) {
selectedSize = gpuTextureSize;
selectedCameraConfig = currentCameraConfig;
}
}
Log.d(&quot;TAG&quot;, &quot;Selected CameraConfig CPU image size:&quot; + selectedCameraConfig.getImageSize() + &quot; GPU texture size:&quot; + selectedCameraConfig.getTextureSize());
session.setCameraConfig(selectedCameraConfig);
// ...
// Don&#39;t forget to configure the session afterwards
session.configure(config);

huangapple
  • 本文由 发表于 2020年9月30日 15:46:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64133110.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定