英文:
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
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;
}
}
答案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<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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论