Browse Source

Kamera Preview ergänzt

Benutzeroberflaeche_Preview_Kamera
Michael Pilhoefer 1 year ago
parent
commit
1d0192083d

+ 3
- 0
app/src/main/AndroidManifest.xml View File

<activity <activity
android:name=".AccelerometerActivity" android:name=".AccelerometerActivity"
android:exported="false" /> android:exported="false" />
<activity
android:name=".VideoPreviewActivity"
android:exported="false" />
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"
android:exported="true"> android:exported="true">

+ 17
- 0
app/src/main/java/com/example/greenwatch/MainActivity.java View File

private Button audiodetectionAndAccelerometerButton; private Button audiodetectionAndAccelerometerButton;
private Button videodetectionAndAccelerometerButton; private Button videodetectionAndAccelerometerButton;
private Button connectionButton; private Button connectionButton;
private Button prewview_kamera;
private MainActivityViewModel mMainActivityViewModel; private MainActivityViewModel mMainActivityViewModel;


@Override @Override
audiodetectionAndAccelerometerButton = (Button) findViewById(R.id.audiodetectionAndAccelerometerButton); audiodetectionAndAccelerometerButton = (Button) findViewById(R.id.audiodetectionAndAccelerometerButton);
videodetectionAndAccelerometerButton = (Button) findViewById(R.id.videodetectionAndAccelerometerButton); videodetectionAndAccelerometerButton = (Button) findViewById(R.id.videodetectionAndAccelerometerButton);
connectionButton = (Button) findViewById(R.id.connectionButton); connectionButton = (Button) findViewById(R.id.connectionButton);
prewview_kamera = (Button) findViewById(R.id.preview_kamera);


RecyclerView deviceListRecyclerView = findViewById(R.id.deviceListRecyclerView); RecyclerView deviceListRecyclerView = findViewById(R.id.deviceListRecyclerView);
deviceListRecyclerView.setLayoutManager(new LinearLayoutManager(this)); deviceListRecyclerView.setLayoutManager(new LinearLayoutManager(this));
} }
}); });


prewview_kamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mMainActivityViewModel.isCameraAccessAllowed(MainActivity.this)) {
openVideoPrewvoewActivity();
}
else {
mMainActivityViewModel.accessRequestCamera(MainActivity.this);
}
}
});
audiodetectionButton.setOnClickListener(new View.OnClickListener() { audiodetectionButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
} }
}); });
} }
public void openVideoPrewvoewActivity() {
Intent intent = new Intent(this, VideoPreviewActivity.class);
startActivity(intent);
}
public void openAudiodetectionActivity(){ public void openAudiodetectionActivity(){
Intent intent = new Intent(this, AudiodetectionActivity.class); Intent intent = new Intent(this, AudiodetectionActivity.class);
startActivity(intent); startActivity(intent);

+ 61
- 0
app/src/main/java/com/example/greenwatch/VideoPreviewActivity.java View File

package com.example.greenwatch;

import android.os.Bundle;
import android.util.Size;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.ImageAnalysis;
import androidx.camera.core.ImageProxy;
import androidx.camera.core.Preview;
import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.camera.view.PreviewView;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.LifecycleOwner;

import com.google.common.util.concurrent.ListenableFuture;

import java.util.concurrent.ExecutionException;

public class VideoPreviewActivity extends AppCompatActivity {

private Button backToMainActivity;
private PreviewView previewView;

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

backToMainActivity = (Button) findViewById(R.id.videodetectorBackToMainActivity);
previewView = findViewById(R.id.previewView);

backToMainActivity.setOnClickListener(v -> finish());


final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(() -> {
try {
bindImageAnalysis(cameraProviderFuture.get());
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}, ContextCompat.getMainExecutor(this));
}

private void bindImageAnalysis(ProcessCameraProvider cameraProvider) {
ImageAnalysis.Builder builder = new ImageAnalysis.Builder();
builder.setTargetResolution(new Size(640, 480));
builder.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST);
ImageAnalysis imageAnalysis = builder.build();
imageAnalysis.setAnalyzer(
ContextCompat.getMainExecutor(this),
ImageProxy::close);

Preview preview = new Preview.Builder().build();
CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
preview.setSurfaceProvider(previewView.getSurfaceProvider());
cameraProvider.bindToLifecycle((LifecycleOwner) this, cameraSelector, imageAnalysis, preview);
}
}

+ 7
- 0
app/src/main/res/layout/activity_main.xml View File

android:padding="10dp" android:padding="10dp"
tools:context=".MainActivity"> tools:context=".MainActivity">


<Button
android:id="@+id/preview_kamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PrewView Kamera" />

<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/alarmHistoryListRecyclerView" android:id="@+id/alarmHistoryListRecyclerView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="3" android:layout_weight="3"
tools:listitem="@layout/device_item"> tools:listitem="@layout/device_item">

</androidx.recyclerview.widget.RecyclerView> </androidx.recyclerview.widget.RecyclerView>
<LinearLayout <LinearLayout

+ 31
- 0
app/src/main/res/layout/activity_preview_kamera.xml View File

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="">

<Button
android:id="@+id/videodetectorBackToMainActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Back to MainActivity">
</Button>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/erklaerung">

<androidx.camera.view.PreviewView
android:id="@+id/previewView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Loading…
Cancel
Save