Skip to content

Commit

Permalink
Android demo add camera permission check
Browse files Browse the repository at this point in the history
  • Loading branch information
gezhaoyou committed Jan 21, 2024
1 parent f984fbf commit b669984
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
62 changes: 51 additions & 11 deletions android/app/src/main/java/com/pixpark/GPUPixel/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package com.pixpark.PixDemo;

import static android.widget.Toast.LENGTH_LONG;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;

import com.pixpark.PixDemo.databinding.ActivityMainBinding;
import com.pixpark.gpupixel.GPUPixel;
Expand All @@ -17,6 +24,9 @@
import com.pixpark.gpupixel.GPUPixelView;

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
// 在Activity中定义一个常量以便在权限请求结果中识别相机权限请求
private static final int CAMERA_PERMISSION_REQUEST_CODE = 200;


// Used to load the 'gpupixel' library on application startup.
static {
Expand All @@ -39,6 +49,7 @@ protected void onCreate(Bundle savedInstanceState) {
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

// get log path
String path = getExternalFilesDir("gpupixel").getAbsolutePath();
Log.i("GPUPixel", path);

Expand All @@ -48,17 +59,6 @@ protected void onCreate(Bundle savedInstanceState) {
// preview
surfaceView = findViewById(R.id.surfaceView);
surfaceView.setMirror(true);
// 美颜滤镜
filter = GPUPixelFilter.create("BeautyFaceFilter");

// camera
sourceCamera = new GPUPixelSourceCamera(this.getApplicationContext());
sourceCamera.addTarget(filter);
filter.addTarget(surfaceView);

// set default value
filter.setProperty("skin_smoothing", 0.5);
filter.setProperty("whiteness", 0.4);

smooth_seekbar = findViewById(R.id.smooth_seekbar);
smooth_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
Expand Down Expand Up @@ -95,8 +95,48 @@ public void onStopTrackingTouch(SeekBar seekBar) {

}
});
//
this.checkCameraPermission();
}

public void startCameraFilter() {
// 美颜滤镜
filter = GPUPixelFilter.create("BeautyFaceFilter");

// camera
sourceCamera = new GPUPixelSourceCamera(this.getApplicationContext());
sourceCamera.addTarget(filter);
filter.addTarget(surfaceView);

// set default value
filter.setProperty("skin_smoothing", 0.5);
filter.setProperty("whiteness", 0.4);
}


public void checkCameraPermission() {
// 检查相机权限是否已授予
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
// 如果未授予相机权限,向用户请求权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
} else {
startCameraFilter();
}

}

// 处理权限请求结果
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == CAMERA_PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startCameraFilter();
} else {
Toast.makeText(this, "No Camera permission!", LENGTH_LONG);
}
}
}


public void surfaceCreated(SurfaceHolder holder) {
Expand Down
4 changes: 2 additions & 2 deletions android/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
android:layout_centerHorizontal="true"
android:layout_marginBottom="80dp"
android:max="10"
android:progress="5" />
android:progress="0" />

<TextView
android:id="@+id/meibai"
Expand All @@ -51,5 +51,5 @@
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp"
android:max="10"
android:progress="4" />
android:progress="0" />
</RelativeLayout>

0 comments on commit b669984

Please sign in to comment.