增加定时功能
This commit is contained in:
parent
5f7c021712
commit
ed9eb13cd0
|
@ -0,0 +1,123 @@
|
||||||
|
<component name="ProjectCodeStyleConfiguration">
|
||||||
|
<code_scheme name="Project" version="173">
|
||||||
|
<JetCodeStyleSettings>
|
||||||
|
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
|
||||||
|
</JetCodeStyleSettings>
|
||||||
|
<codeStyleSettings language="XML">
|
||||||
|
<option name="FORCE_REARRANGE_MODE" value="1" />
|
||||||
|
<indentOptions>
|
||||||
|
<option name="CONTINUATION_INDENT_SIZE" value="4" />
|
||||||
|
</indentOptions>
|
||||||
|
<arrangement>
|
||||||
|
<rules>
|
||||||
|
<section>
|
||||||
|
<rule>
|
||||||
|
<match>
|
||||||
|
<AND>
|
||||||
|
<NAME>xmlns:android</NAME>
|
||||||
|
<XML_ATTRIBUTE />
|
||||||
|
<XML_NAMESPACE>^$</XML_NAMESPACE>
|
||||||
|
</AND>
|
||||||
|
</match>
|
||||||
|
</rule>
|
||||||
|
</section>
|
||||||
|
<section>
|
||||||
|
<rule>
|
||||||
|
<match>
|
||||||
|
<AND>
|
||||||
|
<NAME>xmlns:.*</NAME>
|
||||||
|
<XML_ATTRIBUTE />
|
||||||
|
<XML_NAMESPACE>^$</XML_NAMESPACE>
|
||||||
|
</AND>
|
||||||
|
</match>
|
||||||
|
<order>BY_NAME</order>
|
||||||
|
</rule>
|
||||||
|
</section>
|
||||||
|
<section>
|
||||||
|
<rule>
|
||||||
|
<match>
|
||||||
|
<AND>
|
||||||
|
<NAME>.*:id</NAME>
|
||||||
|
<XML_ATTRIBUTE />
|
||||||
|
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
|
||||||
|
</AND>
|
||||||
|
</match>
|
||||||
|
</rule>
|
||||||
|
</section>
|
||||||
|
<section>
|
||||||
|
<rule>
|
||||||
|
<match>
|
||||||
|
<AND>
|
||||||
|
<NAME>.*:name</NAME>
|
||||||
|
<XML_ATTRIBUTE />
|
||||||
|
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
|
||||||
|
</AND>
|
||||||
|
</match>
|
||||||
|
</rule>
|
||||||
|
</section>
|
||||||
|
<section>
|
||||||
|
<rule>
|
||||||
|
<match>
|
||||||
|
<AND>
|
||||||
|
<NAME>name</NAME>
|
||||||
|
<XML_ATTRIBUTE />
|
||||||
|
<XML_NAMESPACE>^$</XML_NAMESPACE>
|
||||||
|
</AND>
|
||||||
|
</match>
|
||||||
|
</rule>
|
||||||
|
</section>
|
||||||
|
<section>
|
||||||
|
<rule>
|
||||||
|
<match>
|
||||||
|
<AND>
|
||||||
|
<NAME>style</NAME>
|
||||||
|
<XML_ATTRIBUTE />
|
||||||
|
<XML_NAMESPACE>^$</XML_NAMESPACE>
|
||||||
|
</AND>
|
||||||
|
</match>
|
||||||
|
</rule>
|
||||||
|
</section>
|
||||||
|
<section>
|
||||||
|
<rule>
|
||||||
|
<match>
|
||||||
|
<AND>
|
||||||
|
<NAME>.*</NAME>
|
||||||
|
<XML_ATTRIBUTE />
|
||||||
|
<XML_NAMESPACE>^$</XML_NAMESPACE>
|
||||||
|
</AND>
|
||||||
|
</match>
|
||||||
|
<order>BY_NAME</order>
|
||||||
|
</rule>
|
||||||
|
</section>
|
||||||
|
<section>
|
||||||
|
<rule>
|
||||||
|
<match>
|
||||||
|
<AND>
|
||||||
|
<NAME>.*</NAME>
|
||||||
|
<XML_ATTRIBUTE />
|
||||||
|
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
|
||||||
|
</AND>
|
||||||
|
</match>
|
||||||
|
<order>ANDROID_ATTRIBUTE_ORDER</order>
|
||||||
|
</rule>
|
||||||
|
</section>
|
||||||
|
<section>
|
||||||
|
<rule>
|
||||||
|
<match>
|
||||||
|
<AND>
|
||||||
|
<NAME>.*</NAME>
|
||||||
|
<XML_ATTRIBUTE />
|
||||||
|
<XML_NAMESPACE>.*</XML_NAMESPACE>
|
||||||
|
</AND>
|
||||||
|
</match>
|
||||||
|
<order>BY_NAME</order>
|
||||||
|
</rule>
|
||||||
|
</section>
|
||||||
|
</rules>
|
||||||
|
</arrangement>
|
||||||
|
</codeStyleSettings>
|
||||||
|
<codeStyleSettings language="kotlin">
|
||||||
|
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
|
||||||
|
</codeStyleSettings>
|
||||||
|
</code_scheme>
|
||||||
|
</component>
|
|
@ -0,0 +1,5 @@
|
||||||
|
<component name="ProjectCodeStyleConfiguration">
|
||||||
|
<state>
|
||||||
|
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
|
||||||
|
</state>
|
||||||
|
</component>
|
|
@ -1,4 +1,3 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
|
||||||
|
|
|
@ -50,4 +50,5 @@ dependencies {
|
||||||
testImplementation("junit:junit:4.13.2")
|
testImplementation("junit:junit:4.13.2")
|
||||||
androidTestImplementation("androidx.test.ext:junit:1.1.5")
|
androidTestImplementation("androidx.test.ext:junit:1.1.5")
|
||||||
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
|
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
|
||||||
|
implementation ("com.squareup.okhttp3:okhttp:4.12.0")
|
||||||
}
|
}
|
|
@ -9,6 +9,10 @@
|
||||||
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
||||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
||||||
android:maxSdkVersion="28" />
|
android:maxSdkVersion="28" />
|
||||||
|
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
|
||||||
|
android:maxSdkVersion="28" />
|
||||||
|
<uses-permission android:name="android.permission.INTERNET"/>
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||||
|
@ -18,7 +22,9 @@
|
||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
android:theme="@style/Theme.Travebing"
|
android:theme="@style/Theme.Travebing"
|
||||||
|
android:networkSecurityConfig="@xml/network_security_config"
|
||||||
tools:targetApi="31">
|
tools:targetApi="31">
|
||||||
|
|
||||||
<activity
|
<activity
|
||||||
android:name=".MainActivity"
|
android:name=".MainActivity"
|
||||||
android:exported="true">
|
android:exported="true">
|
||||||
|
|
|
@ -2,25 +2,21 @@
|
||||||
|
|
||||||
package com.bing89.travebing
|
package com.bing89.travebing
|
||||||
|
|
||||||
import android.Manifest
|
import android.Manifest.permission
|
||||||
import android.Manifest.*
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.content.ContentValues
|
import android.content.ContentValues
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
import android.location.Criteria
|
|
||||||
import android.location.Location
|
import android.location.Location
|
||||||
import android.location.LocationListener
|
import android.location.LocationListener
|
||||||
import android.location.LocationManager
|
import android.location.LocationManager
|
||||||
import android.os.Build
|
import android.net.Uri
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Handler
|
|
||||||
import android.os.Looper
|
|
||||||
import android.provider.MediaStore
|
import android.provider.MediaStore
|
||||||
import android.util.Base64
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.widget.Button
|
import android.widget.Button
|
||||||
|
import android.widget.EditText
|
||||||
import android.widget.ImageView
|
import android.widget.ImageView
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
|
@ -32,14 +28,28 @@ import androidx.camera.core.ImageCaptureException
|
||||||
import androidx.camera.lifecycle.ProcessCameraProvider
|
import androidx.camera.lifecycle.ProcessCameraProvider
|
||||||
import androidx.core.app.ActivityCompat
|
import androidx.core.app.ActivityCompat
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.core.content.FileProvider
|
import com.bing89.travebing.R.id
|
||||||
import com.bing89.travebing.R.*
|
import com.bing89.travebing.R.layout
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.DelicateCoroutinesApi
|
||||||
import java.io.ByteArrayOutputStream
|
import okhttp3.Call
|
||||||
|
import okhttp3.Callback
|
||||||
|
import okhttp3.MediaType.Companion.toMediaType
|
||||||
|
import okhttp3.MultipartBody
|
||||||
|
import okhttp3.OkHttpClient
|
||||||
|
import okhttp3.Request
|
||||||
|
import okhttp3.RequestBody.Companion.asRequestBody
|
||||||
|
import okhttp3.Response
|
||||||
|
import java.io.BufferedInputStream
|
||||||
|
import java.io.BufferedOutputStream
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.io.FileInputStream
|
||||||
|
import java.io.FileOutputStream
|
||||||
|
import java.io.IOException
|
||||||
|
import java.io.InputStream
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.Date
|
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
|
import java.util.Timer
|
||||||
|
import java.util.TimerTask
|
||||||
|
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
|
@ -48,23 +58,33 @@ class MainActivity : AppCompatActivity() {
|
||||||
private lateinit var previewImageView: ImageView
|
private lateinit var previewImageView: ImageView
|
||||||
private lateinit var gpsInfoTextView: TextView
|
private lateinit var gpsInfoTextView: TextView
|
||||||
private var imageCapture: ImageCapture? = null
|
private var imageCapture: ImageCapture? = null
|
||||||
|
private lateinit var planText: EditText
|
||||||
private var isMonitoring = false
|
private var isMonitoring = false
|
||||||
private var photoFilePath: String? = null
|
private var photoFilePath: String? = null
|
||||||
private var locationManager: LocationManager? = null
|
private var locationManager: LocationManager? = null
|
||||||
|
private lateinit var lat: String
|
||||||
|
private lateinit var lon: String
|
||||||
|
private lateinit var currentImg:String
|
||||||
|
private lateinit var context:Context
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(layout.activity_main)
|
|
||||||
|
|
||||||
|
lat = ""
|
||||||
|
lon = ""
|
||||||
|
currentImg = ""
|
||||||
|
context = baseContext
|
||||||
|
setContentView(layout.activity_main)
|
||||||
startButton = findViewById(id.startButton)
|
startButton = findViewById(id.startButton)
|
||||||
previewImageView = findViewById(id.previewImageView)
|
previewImageView = findViewById(id.previewImageView)
|
||||||
gpsInfoTextView = findViewById(id.gpsInfoTextView)
|
gpsInfoTextView = findViewById(id.gpsInfoTextView)
|
||||||
|
planText = findViewById(id.planText)
|
||||||
startCamera()
|
startCamera()
|
||||||
startButton.setOnClickListener {
|
startButton.setOnClickListener {
|
||||||
if (isMonitoring) {
|
if (isMonitoring) {
|
||||||
stopMonitoring()
|
stopMonitoring()
|
||||||
|
planText.isActivated = true
|
||||||
} else {
|
} else {
|
||||||
|
planText.isActivated = false
|
||||||
startMonitoring()
|
startMonitoring()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,47 +93,94 @@ class MainActivity : AppCompatActivity() {
|
||||||
locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun updateWayCallback(file: File):Callback{
|
||||||
|
return object:Callback{
|
||||||
|
override fun onFailure(call: Call, e: IOException) {
|
||||||
|
file.delete()
|
||||||
|
println("Fail")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onResponse(call: Call, response: Response) {
|
||||||
|
println(response.body.toString())
|
||||||
|
file.delete()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private fun updateWay(img:InputStream){
|
||||||
|
Log.d(TAG, "$img.jpg")
|
||||||
|
val client = OkHttpClient()
|
||||||
|
|
||||||
|
var builder: MultipartBody.Builder = MultipartBody.Builder()
|
||||||
|
.setType(MultipartBody.FORM)
|
||||||
|
.addFormDataPart("lat", lat)
|
||||||
|
.addFormDataPart("lon", lon)
|
||||||
|
val dst = File("${context.externalCacheDir}/tmp")
|
||||||
|
copyfile(img, dst )
|
||||||
|
val requestFile = dst.asRequestBody(MEDIA_TYPE_FILE)
|
||||||
|
builder = builder.addFormDataPart("file", "1.jpg", requestFile)
|
||||||
|
val multipartBody: MultipartBody = builder.build()
|
||||||
|
val request = Request.Builder()
|
||||||
|
.url("http://192.168.0.200:3000/api/v1/plans/" + planText.text + "/ways")
|
||||||
|
.post(multipartBody)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
val call: Call = client.newCall(request)
|
||||||
|
call.enqueue(updateWayCallback(dst))
|
||||||
|
}
|
||||||
private fun startMonitoring() {
|
private fun startMonitoring() {
|
||||||
isMonitoring = true
|
isMonitoring = true
|
||||||
startButton.text = "休息"
|
startButton.text = "休息"
|
||||||
// if(imageCapture == null ){
|
val timer = Timer()
|
||||||
// startCamera()
|
val task = object : TimerTask() {
|
||||||
// }
|
override fun run() {
|
||||||
takePhoto()
|
// 每隔 1 秒钟执行一次此方法
|
||||||
|
// 在这里写下你需要执行的代码
|
||||||
|
takePhoto()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// val intervalMillis = 60000L // 间隔时间,这里设置为1分钟
|
timer.schedule(task, 0, 300000) // 每隔 300 秒钟执行一次
|
||||||
// val handler = Handler(Looper.getMainLooper())
|
|
||||||
//
|
// updateWay(img)
|
||||||
// handler.post(object : Runnable {
|
|
||||||
// override fun run() {
|
|
||||||
// if (isMonitoring) {
|
|
||||||
// getLocation()
|
|
||||||
//// capturePhotoAndUpload()
|
|
||||||
// takePhoto()
|
|
||||||
// handler.postDelayed(this, intervalMillis)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun stopMonitoring() {
|
private fun stopMonitoring() {
|
||||||
isMonitoring = false
|
isMonitoring = false
|
||||||
startButton.text = "出发"
|
startButton.text = "出发"
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequiresPermission(
|
@RequiresPermission(
|
||||||
anyOf = [permission.ACCESS_COARSE_LOCATION, permission.ACCESS_FINE_LOCATION],
|
anyOf = [permission.ACCESS_COARSE_LOCATION,
|
||||||
|
permission.ACCESS_FINE_LOCATION,
|
||||||
|
permission.INTERNET,
|
||||||
|
permission.ACCESS_MEDIA_LOCATION,
|
||||||
|
permission.ACCESS_BACKGROUND_LOCATION,
|
||||||
|
permission.ACCESS_NETWORK_STATE,
|
||||||
|
permission.CAMERA,
|
||||||
|
permission.WRITE_EXTERNAL_STORAGE,
|
||||||
|
permission.READ_EXTERNAL_STORAGE,
|
||||||
|
permission.ACCESS_FINE_LOCATION],
|
||||||
)
|
)
|
||||||
|
|
||||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||||
super.onActivityResult(requestCode, resultCode, data)
|
super.onActivityResult(requestCode, resultCode, data)
|
||||||
if (resultCode == PHOTO_REQUEST_CODE ) {
|
if (resultCode == PHOTO_REQUEST_CODE) {
|
||||||
// doSomeOperations()
|
// doSomeOperations()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkPermissions(): Boolean {
|
private fun checkPermissions(): Boolean {
|
||||||
val requiredPermissions = arrayOf(
|
val requiredPermissions = arrayOf(
|
||||||
|
permission.ACCESS_COARSE_LOCATION,
|
||||||
|
permission.ACCESS_FINE_LOCATION,
|
||||||
|
permission.INTERNET,
|
||||||
|
permission.ACCESS_MEDIA_LOCATION,
|
||||||
|
permission.ACCESS_BACKGROUND_LOCATION,
|
||||||
|
permission.ACCESS_NETWORK_STATE,
|
||||||
permission.CAMERA,
|
permission.CAMERA,
|
||||||
permission.WRITE_EXTERNAL_STORAGE,
|
permission.WRITE_EXTERNAL_STORAGE,
|
||||||
|
permission.READ_EXTERNAL_STORAGE,
|
||||||
permission.ACCESS_FINE_LOCATION
|
permission.ACCESS_FINE_LOCATION
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -123,55 +190,62 @@ class MainActivity : AppCompatActivity() {
|
||||||
permission
|
permission
|
||||||
) != PackageManager.PERMISSION_GRANTED
|
) != PackageManager.PERMISSION_GRANTED
|
||||||
) {
|
) {
|
||||||
ActivityCompat.requestPermissions(this, requiredPermissions, PERMISSION_REQUEST_CODE)
|
ActivityCompat.requestPermissions(
|
||||||
|
this,
|
||||||
|
requiredPermissions,
|
||||||
|
PERMISSION_REQUEST_CODE
|
||||||
|
)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
//define the listener
|
//define the listener
|
||||||
private val locationListener: LocationListener = object : LocationListener {
|
private val locationListener: LocationListener = object : LocationListener {
|
||||||
override fun onLocationChanged(location: Location) {
|
override fun onLocationChanged(location: Location) {
|
||||||
|
lat = location.latitude.toString()
|
||||||
|
lon = location.longitude.toString()
|
||||||
updateGpsInfo(location)
|
updateGpsInfo(location)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {}
|
override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {}
|
||||||
override fun onProviderEnabled(provider: String) {}
|
override fun onProviderEnabled(provider: String) {}
|
||||||
override fun onProviderDisabled(provider: String) {}
|
override fun onProviderDisabled(provider: String) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createPhotoFile(): File? {
|
private fun getLocation() {
|
||||||
val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
|
|
||||||
val storageDir = getExternalFilesDir(null)
|
|
||||||
return File.createTempFile("JPEG_${timeStamp}_", ".jpg", storageDir)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getLocation(){
|
|
||||||
val locMan = getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
val locMan = getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
||||||
val checkCameraPermission = ContextCompat.checkSelfPermission(this, permission.ACCESS_FINE_LOCATION)
|
val checkCameraPermission =
|
||||||
|
ContextCompat.checkSelfPermission(this, permission.ACCESS_FINE_LOCATION)
|
||||||
val checkCallPhonePermission =
|
val checkCallPhonePermission =
|
||||||
ContextCompat.checkSelfPermission(this, permission.ACCESS_COARSE_LOCATION)
|
ContextCompat.checkSelfPermission(this, permission.ACCESS_COARSE_LOCATION)
|
||||||
if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED || checkCameraPermission != PackageManager.PERMISSION_GRANTED) {
|
if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED || checkCameraPermission != PackageManager.PERMISSION_GRANTED) {
|
||||||
ActivityCompat.requestPermissions(this, arrayOf(
|
ActivityCompat.requestPermissions(
|
||||||
permission.ACCESS_COARSE_LOCATION,
|
this, arrayOf(
|
||||||
permission.ACCESS_FINE_LOCATION
|
permission.ACCESS_COARSE_LOCATION,
|
||||||
), 22)
|
permission.ACCESS_FINE_LOCATION
|
||||||
|
), 22
|
||||||
|
)
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
// Request location updates
|
// Request location updates
|
||||||
val providers = locMan.getProviders(false)
|
val providers = locMan.getProviders(false)
|
||||||
var provider = LocationManager.NETWORK_PROVIDER
|
var provider = LocationManager.NETWORK_PROVIDER
|
||||||
if(providers.size != 0) {
|
if (providers.size != 0) {
|
||||||
if (providers.contains(LocationManager.GPS_PROVIDER)) {
|
if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
|
||||||
|
provider = LocationManager.NETWORK_PROVIDER
|
||||||
|
}else if(providers.contains(LocationManager.GPS_PROVIDER)){
|
||||||
provider = LocationManager.GPS_PROVIDER
|
provider = LocationManager.GPS_PROVIDER
|
||||||
}else if (providers.contains(LocationManager.PASSIVE_PROVIDER)){
|
} else if (providers.contains(LocationManager.PASSIVE_PROVIDER)) {
|
||||||
provider = LocationManager.PASSIVE_PROVIDER
|
provider = LocationManager.PASSIVE_PROVIDER
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Toast.makeText(baseContext, "使用" + provider + "定位", Toast.LENGTH_SHORT).show()
|
Toast.makeText(baseContext, "使用" + provider + "定位", Toast.LENGTH_SHORT).show()
|
||||||
Log.d(TAG, "==================>>>> get location from gps")
|
Log.d(TAG, "==================>>>> get location from gps")
|
||||||
locMan.requestLocationUpdates(provider, 10000L, 100f, locationListener)
|
locMan.requestLocationUpdates(provider, 10000L, 100f, locationListener)
|
||||||
} catch(ex: SecurityException) {
|
} catch (ex: SecurityException) {
|
||||||
Log.d(TAG, "Security Exception, no location available")
|
Log.d(TAG, "Security Exception, no location available")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -184,71 +258,23 @@ class MainActivity : AppCompatActivity() {
|
||||||
gpsInfoTextView.text = gpsInfo
|
gpsInfoTextView.text = gpsInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun uploadPhotoAndGpsInfo(photoFilePath: String, gpsInfo: String) {
|
|
||||||
// 在此处实现将照片和GPS信息上传到api.abc.com/report接口的逻辑
|
|
||||||
// 可以使用 Retrofit 或其他网络库进行网络请求
|
|
||||||
// 将照片转换成 Base64 字符串,然后添加到请求中
|
|
||||||
// 请求完成后更新界面上的预览窗口等信息
|
|
||||||
GlobalScope.launch(Dispatchers.IO) {
|
|
||||||
try {
|
|
||||||
val photoFile = File(photoFilePath)
|
|
||||||
val photoBytes = ByteArrayOutputStream().use {
|
|
||||||
val inputStream = photoFile.inputStream()
|
|
||||||
inputStream.copyTo(it)
|
|
||||||
it.toByteArray()
|
|
||||||
}
|
|
||||||
|
|
||||||
val base64Photo = Base64.encodeToString(photoBytes, Base64.DEFAULT)
|
|
||||||
|
|
||||||
// 在此处添加上传逻辑
|
|
||||||
// 使用 Retrofit 或其他网络库发送照片和GPS信息到api.abc.com/report接口
|
|
||||||
// 请注意处理网络请求的结果和错误
|
|
||||||
} catch (e: Exception) {
|
|
||||||
e.printStackTrace()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val PERMISSION_REQUEST_CODE = 1001
|
private const val PERMISSION_REQUEST_CODE = 1001
|
||||||
private const val PHOTO_REQUEST_CODE = 1002
|
private const val PHOTO_REQUEST_CODE = 1002
|
||||||
private const val TAG = "Travebing"
|
private const val TAG = "Travebing"
|
||||||
private const val FILENAME_FORMAT = "yyyy-MM-dd-HH-mm-ss-SSS"
|
private const val FILENAME_FORMAT = "yyyy-MM-dd-HH-mm-ss-SSS"
|
||||||
private const val REQUEST_CODE_PERMISSIONS = 10
|
private const val REQUEST_CODE_PERMISSIONS = 10
|
||||||
|
val MEDIA_TYPE_JSON = "application/json; charset=utf-8".toMediaType()
|
||||||
|
val MEDIA_TYPE_MULTIPART = "multipart/form-data".toMediaType()
|
||||||
|
val MEDIA_TYPE_FILE = "application/octet-stream".toMediaType()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun startCamera() {
|
private fun startCamera() {
|
||||||
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
|
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
|
||||||
|
|
||||||
cameraProviderFuture.addListener({
|
cameraProviderFuture.addListener({
|
||||||
// Used to bind the lifecycle of cameras to the lifecycle owner
|
|
||||||
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
|
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
|
||||||
|
|
||||||
// Preview
|
|
||||||
// val preview = Preview.Builder()
|
|
||||||
// .build()
|
|
||||||
// .also {
|
|
||||||
// it.setSurfaceProvider(viewBinding.viewFinder.surfaceProvider)
|
|
||||||
// }
|
|
||||||
// val recorder = Recorder.Builder()
|
|
||||||
// .setQualitySelector(QualitySelector.from(Quality.HIGHEST,
|
|
||||||
// FallbackStrategy.higherQualityOrLowerThan(Quality.SD)))
|
|
||||||
// .build()
|
|
||||||
// videoCapture = VideoCapture.withOutput(recorder)
|
|
||||||
|
|
||||||
imageCapture = ImageCapture.Builder().build()
|
imageCapture = ImageCapture.Builder().build()
|
||||||
|
|
||||||
/*
|
|
||||||
val imageAnalyzer = ImageAnalysis.Builder().build()
|
|
||||||
.also {
|
|
||||||
setAnalyzer(
|
|
||||||
cameraExecutor,
|
|
||||||
LuminosityAnalyzer { luma -> startCamera()
|
|
||||||
Log.d(TAG, "Average luminosity: $luma")
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Select back camera as a default
|
// Select back camera as a default
|
||||||
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
|
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
|
||||||
|
|
||||||
|
@ -258,17 +284,28 @@ class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
// Bind use cases to camera
|
// Bind use cases to camera
|
||||||
cameraProvider.bindToLifecycle(
|
cameraProvider.bindToLifecycle(
|
||||||
this, cameraSelector, imageCapture)
|
this, cameraSelector, imageCapture
|
||||||
|
)
|
||||||
|
|
||||||
} catch(exc: Exception) {
|
} catch (exc: Exception) {
|
||||||
Log.e(TAG, "Use case binding failed", exc)
|
Log.e(TAG, "Use case binding failed", exc)
|
||||||
}
|
}
|
||||||
|
|
||||||
}, ContextCompat.getMainExecutor(this))
|
}, ContextCompat.getMainExecutor(this))
|
||||||
}
|
}
|
||||||
private fun takePhoto() {
|
/**
|
||||||
|
* 单个文件复制
|
||||||
|
*/
|
||||||
|
fun copyfile(srcFile: InputStream, destFile: File) {
|
||||||
|
val fos = FileOutputStream(destFile)
|
||||||
|
srcFile.copyTo(fos)
|
||||||
|
fos.close()
|
||||||
|
srcFile.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun takePhoto(): String {
|
||||||
// Get a stable reference of the modifiable image capture use case
|
// Get a stable reference of the modifiable image capture use case
|
||||||
val imageCapture = imageCapture ?: return
|
val imageCapture = imageCapture ?: return ""
|
||||||
Log.d(TAG, "================>>>>> now do capture photo")
|
Log.d(TAG, "================>>>>> now do capture photo")
|
||||||
// Create time stamped name and MediaStore entry.
|
// Create time stamped name and MediaStore entry.
|
||||||
val name = SimpleDateFormat(FILENAME_FORMAT, Locale.US)
|
val name = SimpleDateFormat(FILENAME_FORMAT, Locale.US)
|
||||||
|
@ -276,14 +313,16 @@ class MainActivity : AppCompatActivity() {
|
||||||
val contentValues = ContentValues().apply {
|
val contentValues = ContentValues().apply {
|
||||||
put(MediaStore.MediaColumns.DISPLAY_NAME, name)
|
put(MediaStore.MediaColumns.DISPLAY_NAME, name)
|
||||||
put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
|
put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
|
||||||
put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/CameraX-Image")
|
put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/travebing")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create output options object which contains file + metadata
|
// Create output options object which contains file + metadata
|
||||||
val outputOptions = ImageCapture.OutputFileOptions
|
val outputOptions = ImageCapture.OutputFileOptions
|
||||||
.Builder(contentResolver,
|
.Builder(
|
||||||
|
contentResolver,
|
||||||
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
|
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
|
||||||
contentValues)
|
contentValues
|
||||||
|
)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
// Set up image capture listener, which is triggered after photo has
|
// Set up image capture listener, which is triggered after photo has
|
||||||
|
@ -295,16 +334,20 @@ class MainActivity : AppCompatActivity() {
|
||||||
override fun onError(exc: ImageCaptureException) {
|
override fun onError(exc: ImageCaptureException) {
|
||||||
Log.e(TAG, "+++++++++++Photo capture failed: ${exc.message}", exc)
|
Log.e(TAG, "+++++++++++Photo capture failed: ${exc.message}", exc)
|
||||||
}
|
}
|
||||||
|
@SuppressLint("Recycle")
|
||||||
override fun
|
override fun onImageSaved(output: ImageCapture.OutputFileResults) {
|
||||||
onImageSaved(output: ImageCapture.OutputFileResults){
|
|
||||||
previewImageView.setImageURI(output.savedUri)
|
previewImageView.setImageURI(output.savedUri)
|
||||||
val msg = "--------- Photo capture succeeded: ${output.savedUri}"
|
val msg = "--------- Photo capture succeeded: ${output.savedUri}"
|
||||||
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
|
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
|
||||||
Log.d(TAG, msg)
|
Log.d(TAG, msg)
|
||||||
|
val resolve = context.contentResolver
|
||||||
|
val uri = Uri.withAppendedPath(output.savedUri, "")
|
||||||
|
val ins:InputStream = resolve.openInputStream(uri)!!
|
||||||
|
updateWay(ins)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,7 @@
|
||||||
tools:srcCompat="@tools:sample/backgrounds/scenic" />
|
tools:srcCompat="@tools:sample/backgrounds/scenic" />
|
||||||
|
|
||||||
<EditText
|
<EditText
|
||||||
android:id="@+id/editTextText"
|
android:id="@+id/planText"
|
||||||
android:layout_width="262dp"
|
android:layout_width="262dp"
|
||||||
android:layout_height="44dp"
|
android:layout_height="44dp"
|
||||||
android:layout_marginStart="18dp"
|
android:layout_marginStart="18dp"
|
||||||
|
@ -56,7 +56,7 @@
|
||||||
android:layout_marginBottom="104dp"
|
android:layout_marginBottom="104dp"
|
||||||
android:ems="10"
|
android:ems="10"
|
||||||
android:inputType="text"
|
android:inputType="text"
|
||||||
android:text="travel.bing89.com"
|
android:text="1"
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<network-security-config>
|
||||||
|
<base-config cleartextTrafficPermitted="true"/>
|
||||||
|
</network-security-config>
|
Loading…
Reference in New Issue