You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
TermApp/app/src/main/java/com/xinyingpower/microphoto/MainActivity.java

241 lines
8.2 KiB
Java

package com.xinyingpower.microphoto;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.SystemClock;
2 years ago
import androidx.core.app.ActivityCompat;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.dowse.camera.client.DSCameraManager;
import com.xinyingpower.microphoto.databinding.ActivityMainBinding;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
private static int MY_PERMISSIONS_REQUEST_FOREGROUND_SERVICE = 100;
// Used to load the 'microphoto' library on application startup.
static {
System.loadLibrary("microphoto");
}
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
this.binding.start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String[] accessPermissions = new String[] {
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.FOREGROUND_SERVICE
};
boolean needRequire = false;
for(String access : accessPermissions) {
int curPermission = ActivityCompat.checkSelfPermission(MainActivity.this, access);
if(curPermission != PackageManager.PERMISSION_GRANTED) {
needRequire = true;
break;
}
}
if (needRequire) {
ActivityCompat.requestPermissions(
MainActivity.this,
accessPermissions,
MY_PERMISSIONS_REQUEST_FOREGROUND_SERVICE);
return;
}
int channel = 2;
String path = buildPhotoDir(channel);
String fileName = buildPhotoFileName(channel, 255);
// MainActivity.this.takePhoto(channel, 255, path, fileName);
Intent intent = new Intent(MainActivity.this, MicroPhotoService.class);
intent.setAction(MicroPhotoService.ACTION_START);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
MainActivity.this.startForegroundService(intent);
} else {
MainActivity.this.startService(intent);
}
binding.start.setEnabled(false);
binding.stop.setEnabled(true);
}
});
this.binding.stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, MicroPhotoService.class);
intent.setAction(MicroPhotoService.ACTION_STOP);
MainActivity.this.stopService(intent);
binding.start.setEnabled(true);
binding.stop.setEnabled(false);
}
});
Handler handler=new Handler();
Runnable runnable=new Runnable(){
@Override
public void run() {
binding.start.performClick();
}
};
2 years ago
handler.postDelayed(runnable, 1000);
}
protected void takePhoto()
{
File path = Environment.getExternalStorageDirectory();
File file = new File(path, "photo.jpg");
boolean res = false;
res = DSCameraManager.getInstace().init();
res = DSCameraManager.getInstace().takePhoto(file.getAbsolutePath(), 2);
if (!res)
{
int aa = 0;
}
res = DSCameraManager.getInstace().unInit();
}
String buildPhotoDir(int channel) {
File path = new File(Environment.getExternalStorageDirectory(), "com.xinyingpower.com/photos/");
if (!path.exists() && !path.mkdirs()) {
return null;
}
String p = path.getAbsolutePath();
if (!p.endsWith(File.separator)) {
p += File.separator;
}
return p;
}
String buildPhotoFileName(int channel, int preset) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "img_" + Integer.toString(channel) + "_" + Integer.toHexString(preset).toUpperCase() + "_" + date + ".jpg";
return photoFile;
}
private void takePhoto(int aa) {
System.out.println("Preparing to take photo");
Camera camera = null;
int cameraCount = 0;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
SystemClock.sleep(1000);
Camera.getCameraInfo(camIdx, cameraInfo);
try {
camera = Camera.open(camIdx);
} catch (RuntimeException e) {
System.out.println("Camera not available: " + camIdx);
camera = null;
//e.printStackTrace();
}
try {
if (null == camera) {
System.out.println("Could not get camera instance");
} else {
System.out.println("Got the camera, creating the dummy surface texture");
//SurfaceTexture dummySurfaceTextureF = new SurfaceTexture(0);
try {
//camera.setPreviewTexture(dummySurfaceTextureF);
camera.setPreviewTexture(new SurfaceTexture(0));
camera.startPreview();
} catch (Exception e) {
System.out.println("Could not set the surface preview texture");
e.printStackTrace();
}
camera.takePicture(null, null, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File path = getApplicationContext().getFilesDir();
// String appPath = path.getAbsolutePath();
File pictureFileDir = path;
if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
return;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "PictureFront_" + "_" + date + ".jpg";
String filename = pictureFileDir.getPath() + File.separator + photoFile;
File mainPicture = new File(filename);
// addImageFile(mainPicture);
try {
FileOutputStream fos = new FileOutputStream(mainPicture);
fos.write(data);
fos.close();
System.out.println("image saved");
} catch (Exception error) {
System.out.println("Image could not be saved");
}
camera.release();
}
});
}
} catch (Exception e) {
camera.release();
}
}
}
/**
* A native method that is implemented by the 'microphoto' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
public native boolean takePhoto(int channel, int preset, String path, String fileName);
}