As per the title, I would like to start the camera on SurfaceView as soon as the application is started, and I always want to keep previewing, but I use MediaRecorder to provide recording functionality.
It's been a little over two months since I first started Android, so I don't know anything about it, but as a feeling,
When using the Camera API or MediaRecorder API, I feel that it is not suitable.There are several things I would like to improve with the current code, so please help me to improve it.
What you want to improve
①I want to always display the camera image in SurfaceView from startup (even after recording)
②I want to get the image that the camera will get from the in-camera (sub-camera?)
③Implement auto-focus functionality and always do auto-focus
が and は have found several large samples depending on the Camera API, but I would like to improve them somehow.Thank you for your cooperation.
public class MainActivity extensions AppCompatActivity implements SurfaceHolder.Callback{
static MediaRecorder mRecorder;
static Camera;
static SurfaceHolder;
Button button 1;
TextView text1;
SurfaceView mSurfaceView1;
static boolean isRecording = false;
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mRecorder = new MediaRecorder();
setContentView(R.layout.activity_main);
initRecorder();
mSurfaceView1=(SurfaceView) findViewById (R.id.surfaceView1);
button1 = (Button) findViewById (R.id.button1);
text1 = (TextView) findViewById (R.id.textView1);
text1.setText("Now Standing By...");
mHolder=mSurfaceView1.getHolder();
mHolder.addCallback(this);
button1.setOnClickListener(newView.OnClickListener(){
@ Override
public void onClick (View v) {
if(isRecording){
mRecorder.stop();
isRecording = false;
initRecorder();
prepareRecorder();
text1.setText("Stop Recording");
button1.setText("Rec●");
} else{
text1.setText("Now Recording");
isRecording = true;
mRecorder.start();
button1.setText("Stop Rec");
}
}
});
}
public Camera getCameraInstance(){
mCamera=null;
try{
mCamera=Camera.open(1);
} catch(Exceptione){
e.printStackTrace();
}
return mCamera;
}
private void initRecorder(){
mRecorder.setAudioSource (MediaRecorder.AudioSource.CAMCORDER);
mRecorder.setVideoSource (MediaRecorder.VideoSource.CAMERA);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setAudioEncoder (MediaRecorder.AudioEncoder.AAC);
mRecorder.setVideoEncoder (MediaRecorder.VideoEncoder.H264);
// recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File pathExternalPublicDir=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM+File.separator+"VID_"+timeStamp+".mp4");
String dir=pathExternalPublicDir.getPath();
mRecorder.setOutputFile(dir);
mRecorder.setMaxDuration (60*1000);
mRecorder.setMaxFileSize (50*1000*1000);
}
private void prepareRecorder(){
mRecorder.setPreviewDisplay(mHolder.getSurface());
try{
mRecorder.prepare();
}catch(IllegalStateExceptione){
e.printStackTrace();
finish();
}catch(IOExceptione){
e.printStackTrace();
finish();
}
}
@ Override
public void surfaceCreated (SurfaceHolder holder) {
prepareRecorder();
try{
Camera.Parameters parameters=mCamera.getParameters();
List<String>focusList=params.getSupportedFocusModes();
for(inti=0;i<focusList.size();i++){
Log.d("CameraFocusMode:", focusList.get(i));
}
param.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);
mCamera.setParameters(params);
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
mCamera.autoFocus(mAutoFocusListener);
}catch(Exceptione) {e.printStackTrace();}
}
private Camera.AutoFocusCallback mAutoFocusListener=new Camera.AutoFocusCallback(){
public void onAutoFocus(boolean success, Camera camera) {
camera.takePicture(null, null, null);
}
};
@ Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h){
}
@ Override
public void surfaceDestroyed (SurfaceHolder holder) {
if(isRecording){
mRecorder.stop();
isRecording = false;
}
mRecorder.release();
finish();
}
(Partial excerpt)
android java camera
This is an old question, but I would like to give a hint to people who come here.
With the current version of AndroidX, you can easily do many things with CameraX, and preview is one of them, see the documentation:
https://developer.android.com/training/camerax/preview
© 2024 OneMinuteCode. All rights reserved.