import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.UsesPermissions;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
@DesignerComponent(
version = 1,
description = "Extensão para capturar imagens da câmera, duplicá-las e exibi-las em tempo real.",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = ""
)
@SimpleObject(external = true)
@UsesPermissions(permissionNames = "android.permission.CAMERA, android.permission.WRITE_EXTERNAL_STORAGE")
public class CameraSplit extends AndroidNonvisibleComponent {
private final Activity activity;
private LinearLayout containerLayout;
private ImageView imageView1;
private ImageView imageView2;
public CameraSplit(ComponentContainer container) {
super(container.$form());
this.activity = (Activity) container.$context();
}
@SimpleFunction(description = "Inicializa os componentes visuais para exibição de imagens.")
public void Initialize() {
activity.runOnUiThread(() -> {
containerLayout = new LinearLayout(activity);
containerLayout.setOrientation(LinearLayout.HORIZONTAL);
imageView1 = new ImageView(activity);
imageView2 = new ImageView(activity);
containerLayout.addView(imageView1);
containerLayout.addView(imageView2);
activity.setContentView(containerLayout);
});
}
@SimpleFunction(description = "Recebe uma imagem capturada, duplica e exibe em duas partes da tela.")
public void ProcessImage(Bitmap inputImage) {
if (inputImage != null) {
activity.runOnUiThread(() -> {
Bitmap duplicatedImage = duplicateImage(inputImage);
imageView1.setImageBitmap(inputImage);
imageView2.setImageBitmap(duplicatedImage);
});
}
}
private Bitmap duplicateImage(Bitmap original) {
int width = original.getWidth();
int height = original.getHeight();
Bitmap duplicated = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(duplicated);
Matrix matrix = new Matrix();
matrix.postTranslate(width, 0);
canvas.drawBitmap(original, 0, 0, null);
canvas.drawBitmap(original, matrix, null);
return duplicated;
}
}
I have to ask why ?
You could easily replicate this with a canvas after taking a picture.
In fact, I want to capture the camera in real time and divide it on the screen
I need an extension for the inventor app that captures the image from the camera and shows it on the screen in real time. But it needs to show duplicate, one on one half of the screen and another on the other half.
You might have a look at the source code for the Procam extension for ideas.
I am not sure if it is possible to duplicate the live feed from the camera, though.