gpt4 book ai didi

android - 我正在尝试在 android 中拍摄连续照片

转载 作者:太空狗 更新时间:2023-10-29 14:54:53 26 4
gpt4 key购买 nike

在这里,我尝试使用单击按钮和服务在后台拍摄连续照片。我可以连续拍照。我以这样的方式编写代码,它必须在我单击按钮后才能工作,但每当我尝试关闭应用程序时,服务都会自行启动。请帮助我解决错误。

服务类:

public class RecorderService extends Service implements     
SurfaceHolder.Callback {
private WindowManager windowManager;
private SurfaceView surfaceView;
private Camera camera = null;

@Override
public void onCreate() {
showMessage("Entered");
// Create new SurfaceView, set its size to 1x1, move it to the top left
corner and set this service as a callback
windowManager = (WindowManager)
this.getSystemService(Context.WINDOW_SERVICE);
surfaceView = new SurfaceView(this);
WindowManager.LayoutParams layoutParams = new
WindowManager.LayoutParams(
1, 1,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT
);
layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
windowManager.addView(surfaceView, layoutParams);
surfaceView.getHolder().addCallback(this);
showMessage("Exited Oncreate");

}

// Method called right after Surface created (initializing and starting
MediaRecorder)
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try {
camera = Camera.open();
showMessage("Opened camera");
try {
camera.setPreviewDisplay(surfaceHolder);
} catch (IOException e) {
throw new RuntimeException(e);
}
camera.startPreview();
showMessage("Started preview");
new CountDownTimer(25000, 5000) {
@Override
public void onFinish() {
camera.lock();
camera.release();
windowManager.removeView(surfaceView);
showMessage("Finished preview");
}
@Override
public void onTick(long millisUntilFinished) {
camera.takePicture(null, null, new Camera.PictureCallback()
{
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
showMessage("No picture");
return;
}
try {
showMessage("Picture present");
FileOutputStream fos = new
FileOutputStream(pictureFile);
fos.write(data);
showMessage("writing Done");
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
showMessage("Took picture");
Toast.makeText(getApplicationContext(), "Picture
Captured",
Toast.LENGTH_SHORT).show();
}
});
}
}.start();
}
catch (Exception e) {
if (camera != null)
camera.release();
throw new RuntimeException(e);
}
}
private File getOutputMediaFile() {
File mediaStorageDir = new

File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_PICTURES),
"MyCameraApp");
showMessage("Picture present");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");

return mediaFile;
}
private static void showMessage(String message) {
Log.i("Camera", message);
}

// Stop recording and remove SurfaceView
@Override
public void onDestroy() {
showMessage("Entered OnDestroy");
camera.lock();
camera.release();
windowManager.removeView(surfaceView);
showMessage("Exited OnDestroy");
}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int
width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}

@Override
public IBinder onBind(Intent intent) {
return null;
}
}

启动按钮类:

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
RecorderService.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(intent);
}
});

Android list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xxxx.xxxx" >

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".PhotoCapture"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service android:name=".RecorderService"/>
<receiver android:name="com.example.xxxx.xxxx">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF">
</action>
<action android:name="android.intent.action.SCREEN_ON"></action>
<action
android:name="android.intent.action.ACTION_POWER_CONNECTED"></action>
<action
android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
<action android:name="android.intent.action.ACTION_SHUTDOWN">
</action>
</intent-filter>
</receiver>
</application>



</manifest>

最佳答案

添加停止服务

stopService(new Intent(xxxx.this, service.class));

关于android - 我正在尝试在 android 中拍摄连续照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32494728/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com