Skip to content

Commit f664cd1

Browse files
committed
Setting camera texture
1 parent 06f97b4 commit f664cd1

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

app/src/main/java/com/android/example/camerax/tflite/CameraActivity.kt

+27-25
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ class CameraActivity : AppCompatActivity(), GLSurfaceView.Renderer {
8888
ImageProcessor.Builder()
8989
.add(ResizeWithCropOrPadOp(cropSize, cropSize))
9090
.add(
91-
ResizeOp(
92-
tfInputSize.height, tfInputSize.width, ResizeOp.ResizeMethod.NEAREST_NEIGHBOR
93-
)
91+
ResizeOp(
92+
tfInputSize.height, tfInputSize.width, ResizeOp.ResizeMethod.NEAREST_NEIGHBOR
93+
)
9494
)
9595
.add(Rot90Op(imageRotationDegrees / 90))
9696
.add(NormalizeOp(0f, 1f))
@@ -100,8 +100,8 @@ class CameraActivity : AppCompatActivity(), GLSurfaceView.Renderer {
100100

101101
private val tflite by lazy {
102102
Interpreter(
103-
FileUtil.loadMappedFile(this, MODEL_PATH),
104-
Interpreter.Options().addDelegate(NnApiDelegate())
103+
FileUtil.loadMappedFile(this, MODEL_PATH),
104+
Interpreter.Options().addDelegate(NnApiDelegate())
105105
)
106106
}
107107

@@ -172,9 +172,9 @@ class CameraActivity : AppCompatActivity(), GLSurfaceView.Renderer {
172172

173173
// Set up the view finder use case to display camera preview
174174
val preview = Preview.Builder()
175-
.setTargetAspectRatio(AspectRatio.RATIO_4_3)
176-
.setTargetRotation(view_finder.display.rotation)
177-
.build()
175+
.setTargetAspectRatio(AspectRatio.RATIO_4_3)
176+
.setTargetRotation(view_finder.display.rotation)
177+
.build()
178178

179179
// Obtain the current frame from ARSession. When the configuration is set to
180180
// UpdateMode.BLOCKING (it is by default), this will throttle the rendering to the
@@ -271,7 +271,7 @@ class CameraActivity : AppCompatActivity(), GLSurfaceView.Renderer {
271271
// Apply declared configs to CameraX using the same lifecycle owner
272272
cameraProvider.unbindAll()
273273
val camera = cameraProvider.bindToLifecycle(
274-
this as LifecycleOwner, cameraSelector, preview/*, imageAnalysis*/
274+
this as LifecycleOwner, cameraSelector, preview/*, imageAnalysis*/
275275
)
276276

277277
// Use the camera object to link our preview use case with the view
@@ -316,10 +316,10 @@ class CameraActivity : AppCompatActivity(), GLSurfaceView.Renderer {
316316

317317
// Step 1: map location to the preview coordinates
318318
val previewLocation = RectF(
319-
location.left * view_finder.width,
320-
location.top * view_finder.height,
321-
location.right * view_finder.width,
322-
location.bottom * view_finder.height
319+
location.left * view_finder.width,
320+
location.top * view_finder.height,
321+
location.right * view_finder.width,
322+
location.bottom * view_finder.height
323323
)
324324

325325
// Step 2: compensate for camera sensor orientation and mirroring
@@ -330,10 +330,10 @@ class CameraActivity : AppCompatActivity(), GLSurfaceView.Renderer {
330330
(isFrontFacing && !isFlippedOrientation)
331331
) {
332332
RectF(
333-
view_finder.width - previewLocation.right,
334-
view_finder.height - previewLocation.bottom,
335-
view_finder.width - previewLocation.left,
336-
view_finder.height - previewLocation.top
333+
view_finder.width - previewLocation.right,
334+
view_finder.height - previewLocation.bottom,
335+
view_finder.width - previewLocation.left,
336+
view_finder.height - previewLocation.top
337337
)
338338
} else {
339339
previewLocation
@@ -346,17 +346,17 @@ class CameraActivity : AppCompatActivity(), GLSurfaceView.Renderer {
346346
val midY = (rotatedLocation.top + rotatedLocation.bottom) / 2f
347347
return if (view_finder.width < view_finder.height) {
348348
RectF(
349-
midX - (1f + margin) * requestedRatio * rotatedLocation.width() / 2f,
350-
midY - (1f - margin) * rotatedLocation.height() / 2f,
351-
midX + (1f + margin) * requestedRatio * rotatedLocation.width() / 2f,
352-
midY + (1f - margin) * rotatedLocation.height() / 2f
349+
midX - (1f + margin) * requestedRatio * rotatedLocation.width() / 2f,
350+
midY - (1f - margin) * rotatedLocation.height() / 2f,
351+
midX + (1f + margin) * requestedRatio * rotatedLocation.width() / 2f,
352+
midY + (1f - margin) * rotatedLocation.height() / 2f
353353
)
354354
} else {
355355
RectF(
356-
midX - (1f - margin) * rotatedLocation.width() / 2f,
357-
midY - (1f + margin) * requestedRatio * rotatedLocation.height() / 2f,
358-
midX + (1f - margin) * rotatedLocation.width() / 2f,
359-
midY + (1f + margin) * requestedRatio * rotatedLocation.height() / 2f
356+
midX - (1f - margin) * rotatedLocation.width() / 2f,
357+
midY - (1f + margin) * requestedRatio * rotatedLocation.height() / 2f,
358+
midX + (1f - margin) * rotatedLocation.width() / 2f,
359+
midY + (1f + margin) * requestedRatio * rotatedLocation.height() / 2f
360360
)
361361
}
362362
}
@@ -445,6 +445,8 @@ class CameraActivity : AppCompatActivity(), GLSurfaceView.Renderer {
445445
val approximateDistanceMeters = 2.0f
446446
// Performs a ray cast given a screen tap position.
447447
val results = frame.hitTestInstantPlacement(0F, 0F, approximateDistanceMeters)
448+
val displayRotation = display!!.rotation
449+
session.setDisplayGeometry(displayRotation, 256, 256)
448450
if (results.isNotEmpty()) {
449451
val point = results[0].trackable as InstantPlacementPoint
450452
// Create an Anchor from the point's pose.

0 commit comments

Comments
 (0)