From 4c12440dc6e63c45465e7117e62ba03f9b3e3a35 Mon Sep 17 00:00:00 2001 From: Ralf Kistner Date: Fri, 23 Oct 2015 16:51:06 +0200 Subject: Experimental SurfaceTexture support. --- sample/src/main/java/example/zxing/MainActivity.java | 4 ++++ .../src/main/java/example/zxing/SmallCaptureActivity.java | 15 +++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 sample/src/main/java/example/zxing/SmallCaptureActivity.java (limited to 'sample/src/main/java/example') diff --git a/sample/src/main/java/example/zxing/MainActivity.java b/sample/src/main/java/example/zxing/MainActivity.java index cc568be7..55be64f8 100644 --- a/sample/src/main/java/example/zxing/MainActivity.java +++ b/sample/src/main/java/example/zxing/MainActivity.java @@ -57,6 +57,10 @@ public class MainActivity extends ActionBarActivity { new IntentIntegrator(this).setCaptureActivity(CustomScannerActivity.class).initiateScan(); } + public void scanMarginScanner(View view) { + new IntentIntegrator(this).setCaptureActivity(SmallCaptureActivity.class).initiateScan(); + } + @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); diff --git a/sample/src/main/java/example/zxing/SmallCaptureActivity.java b/sample/src/main/java/example/zxing/SmallCaptureActivity.java new file mode 100644 index 00000000..87b6b0ce --- /dev/null +++ b/sample/src/main/java/example/zxing/SmallCaptureActivity.java @@ -0,0 +1,15 @@ +package example.zxing; + +import com.journeyapps.barcodescanner.CaptureActivity; +import com.journeyapps.barcodescanner.CompoundBarcodeView; + +/** + * This activity has a margin. + */ +public class SmallCaptureActivity extends CaptureActivity { + @Override + protected CompoundBarcodeView initializeContent() { + setContentView(R.layout.capture_small); + return (CompoundBarcodeView)findViewById(R.id.zxing_barcode_scanner); + } +} -- cgit v1.2.3 From 656363067db316e02966fd360f9f01a4293bb632 Mon Sep 17 00:00:00 2001 From: Ralf Kistner Date: Mon, 26 Oct 2015 09:49:35 +0200 Subject: Fix centering of texture preview. --- .../src/main/java/example/zxing/MainActivity.java | 5 +- .../journeyapps/barcodescanner/CameraPreview.java | 73 +++++++++++++--------- 2 files changed, 49 insertions(+), 29 deletions(-) (limited to 'sample/src/main/java/example') diff --git a/sample/src/main/java/example/zxing/MainActivity.java b/sample/src/main/java/example/zxing/MainActivity.java index 55be64f8..ba536a32 100644 --- a/sample/src/main/java/example/zxing/MainActivity.java +++ b/sample/src/main/java/example/zxing/MainActivity.java @@ -58,7 +58,10 @@ public class MainActivity extends ActionBarActivity { } public void scanMarginScanner(View view) { - new IntentIntegrator(this).setCaptureActivity(SmallCaptureActivity.class).initiateScan(); + IntentIntegrator integrator = new IntentIntegrator(this); + integrator.setOrientationLocked(false); + integrator.setCaptureActivity(SmallCaptureActivity.class); + integrator.initiateScan(); } @Override diff --git a/zxing-android-embedded/src/com/journeyapps/barcodescanner/CameraPreview.java b/zxing-android-embedded/src/com/journeyapps/barcodescanner/CameraPreview.java index 409726ae..3869c20a 100644 --- a/zxing-android-embedded/src/com/journeyapps/barcodescanner/CameraPreview.java +++ b/zxing-android-embedded/src/com/journeyapps/barcodescanner/CameraPreview.java @@ -372,39 +372,56 @@ public class CameraPreview extends ViewGroup { } } - private void startPreviewIfReady() { - if (currentSurfaceSize != null && previewSize != null && surfaceRect != null) { - if (surfaceView != null && currentSurfaceSize.equals(new Size(surfaceRect.width(), surfaceRect.height()))) { - startCameraPreview(new CameraSurface(surfaceView.getHolder())); - } else if(textureView != null && Build.VERSION.SDK_INT >= 14 && textureView.getSurfaceTexture() != null) { - if(previewSize != null) { - // TODO: fix this math - float ratioSurface = (float) textureView.getWidth() / (float) textureView.getHeight(); - float ratioPreview = (float) previewSize.width / (float) previewSize.height; + /** + * Calculate transformation for the TextureView. + * + * An identity matrix would cause the preview to be scaled up/down to fill the TextureView. + * + * @param textureSize the size of the textureView + * @param previewSize the camera preview resolution + * @return the transform matrix for the TextureView + */ + protected Matrix calculateTextureTransform(Size textureSize, Size previewSize) { + float ratioTexture = (float) textureSize.width / (float) textureSize.height; + float ratioPreview = (float) previewSize.width / (float) previewSize.height; + + float scaleX; + float scaleY; + + // We scale so that either width or height fits exactly in the TextureView, and the other + // is bigger (cropped). + if (ratioTexture < ratioPreview) { + scaleX = ratioPreview / ratioTexture; + scaleY = 1; + } else { + scaleX = 1; + scaleY = ratioTexture / ratioPreview; + } - float scaleX; - float scaleY; + Matrix matrix = new Matrix(); - if (ratioSurface < ratioPreview) { - scaleX = ratioPreview / ratioSurface; - scaleY = 1; - } else { - scaleX = 1; - scaleY = ratioSurface / ratioPreview; - } + matrix.setScale(scaleX, scaleY); - Matrix matrix = new Matrix(); + // Center the preview + float scaledWidth = textureSize.width * scaleX; + float scaledHeight = textureSize.height * scaleY; + float dx = (textureSize.width - scaledWidth) / 2; + float dy = (textureSize.height - scaledHeight) / 2; - matrix.setScale(scaleX, scaleY); + // Perform the translation on the scaled preview + matrix.postTranslate(dx, dy); - float scaledWidth = textureView.getWidth() * scaleX; - float scaledHeight = textureView.getHeight() * scaleY; - float dx = (textureView.getWidth() - scaledWidth) / 2; - float dy = (textureView.getHeight() - scaledHeight) / 2; + return matrix; + } - // TODO: fix translation -// matrix.setTranslate(-dx, -dy); - textureView.setTransform(matrix); + private void startPreviewIfReady() { + if (currentSurfaceSize != null && previewSize != null && surfaceRect != null) { + if (surfaceView != null && currentSurfaceSize.equals(new Size(surfaceRect.width(), surfaceRect.height()))) { + startCameraPreview(new CameraSurface(surfaceView.getHolder())); + } else if(textureView != null && Build.VERSION.SDK_INT >= 14 && textureView.getSurfaceTexture() != null) { + if(previewSize != null) { + Matrix transform = calculateTextureTransform(new Size(textureView.getWidth(), textureView.getHeight()), previewSize); + textureView.setTransform(transform); } startCameraPreview(new CameraSurface(textureView.getSurfaceTexture())); @@ -520,7 +537,7 @@ public class CameraPreview extends ViewGroup { cameraInstance = null; previewActive = false; } - if (currentSurfaceSize == null) { + if (currentSurfaceSize == null && surfaceView != null) { SurfaceHolder surfaceHolder = surfaceView.getHolder(); surfaceHolder.removeCallback(surfaceCallback); } -- cgit v1.2.3