2016-03-06 60 views
0

我需要设置Mobile Vision barcode scanner与小SurfaceView(屏幕高度的1/4)的相机预览。如何按比例做到这一点,而不缩小相机预览?小视野移动视觉

+0

您可以找到调整SurfaceView和TextureView大小以符合Grafika中视频宽高比的视频播放器示例(https://github.com/google/grafika)。特别注意使用AspectFrameLayout来修改SurfaceView的大小。从你的问题来看,我不清楚这是否是你想要的,因为你谈论缩小预览到1/4高度,然后说你不想缩小预览。 – fadden

回答

1

我这样解决了。在我的活动

新增摄影机视图

<com.superup.smartshelf.test.CroppedCameraPreview 
     android:id="@+id/preview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

创建CroppedCameraPreview类

public class CroppedCameraPreview extends ViewGroup { 

    private static final String TAG = "qwe"; 
    private SurfaceView mSurfaceView; 
    private boolean mStartRequested; 
    private boolean mSurfaceAvailable; 
    private CameraSource mCameraSource; 
    private BarcodeDetector barcodeDetector; 

    public CroppedCameraPreview(final Context context, AttributeSet attrs) { 
     super(context, attrs); 

     mSurfaceView = new SurfaceView(context); 
     mSurfaceView.getHolder().addCallback(new SurfaceCallback()); 
     addView(mSurfaceView); 
    } 


    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int croppedWidth = getResources().getDisplayMetrics().widthPixels; 
     int croppedHeight = getResources().getDisplayMetrics().heightPixels/4; 

     setMeasuredDimension(croppedWidth, croppedHeight); 
    } 

    @Override 
    protected void onLayout(boolean changed, int l, int t, int r, int b) { 
     // crop view 
     int actualPreviewWidth = getResources().getDisplayMetrics().widthPixels; 
     int actualPreviewHeight = getResources().getDisplayMetrics().heightPixels; 


     if (mSurfaceView != null) { 
      mSurfaceView.layout(0, 0, actualPreviewWidth, actualPreviewHeight); 
     } 
    } 

    public void start(CameraSource cameraSource) throws IOException { 
     if (cameraSource == null) { 
      stop(); 
     } 

     mCameraSource = cameraSource; 

     if (mCameraSource != null) { 
      mStartRequested = true; 
      startIfReady(); 
     } 
    } 

    public void stop() { 
     if (mCameraSource != null) { 
      mCameraSource.stop(); 
     } 
    } 

    public void release() { 
     if (mCameraSource != null) { 
      mCameraSource.release(); 
      mCameraSource = null; 
     } 
    } 

    private void startIfReady() throws IOException { 
     if (mStartRequested && mSurfaceAvailable) { 
      mCameraSource.start(mSurfaceView.getHolder()); 
      /*if (mOverlay != null) { 
       Size size = mCameraSource.getPreviewSize(); 
       int min = Math.min(size.getWidth(), size.getHeight()); 
       int max = Math.max(size.getWidth(), size.getHeight()); 
       if (isPortraitMode()) { 
        // Swap width and height sizes when in portrait, since it will be rotated by 
        // 90 degrees 
        mOverlay.setCameraInfo(min, max, mCameraSource.getCameraFacing()); 
       } else { 
        mOverlay.setCameraInfo(max, min, mCameraSource.getCameraFacing()); 
       } 
       mOverlay.clear(); 
      }*/ 
      mStartRequested = false; 
     } 
    } 

    private class SurfaceCallback implements SurfaceHolder.Callback { 
     @Override 
     public void surfaceCreated(SurfaceHolder surfaceHolder) { 
      try { 
       mCameraSource.start(mSurfaceView.getHolder()); 
      } catch (IOException e) { 
       Log.e(TAG, "Could not start camera source.", e); 
      } 
     } 

     @Override 
     public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) { 

     } 

     @Override 
     public void surfaceDestroyed(SurfaceHolder surfaceHolder) { 
      mCameraSource.stop(); 

     } 
    } 

} 

而且在我的活动初始化它。 setLayoutParams允许设置视图的大小。

CroppedCameraPreview mPreview = (CroppedCameraPreview) findViewById(R.id.preview); 

     // get display size 
     final DisplayMetrics metrics = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(metrics); 

     //set camera view on 1/4 of the screen 
     LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(metrics.widthPixels, metrics.heightPixels/4); 
     mPreview.setLayoutParams(layoutParams); 

    BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this).build(); 
    BarcodeTrackerFactory barcodeFactory = new BarcodeTrackerFactory(
      new BarcodeTracker.NewDetectionListener() { 
       @Override 
       public void onNewDetection(Barcode barcode) { 
        final String rawBarcode = barcode.rawValue; 
        mPreview.post(new Runnable() { 
         public void run() { 
          String barcode = rawBarcode; 
          mediaPlayer.start(); 
          mPreview.setVisibility(View.GONE); 
          Log.d(TAG, barcode); 

          // do all what you want with barcode 

          } else { 
           Toast.makeText(getApplicationContext(), "Barcode not found", Toast.LENGTH_SHORT).show(); 
          } 

         } 
        }); 
       } 
      }); 
    barcodeDetector.setProcessor(new MultiProcessor.Builder<>(barcodeFactory).build()); 


    mCameraSource = new CameraSource 
      .Builder(this, barcodeDetector) 
      .setAutoFocusEnabled(true) 
      .build(); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    startCameraSource(); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    mPreview.stop(); 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    mCameraSource.release(); 
} 

private void startCameraSource() { 
    try { 
     mPreview.start(mCameraSource); 
    } catch (IOException e) { 
     Log.e(TAG, "Unable to start camera source.", e); 
     mCameraSource.release(); 
     mCameraSource = null; 
    } 
} 
0

只需使用imageView并将图像放入src属性中即可。 这将做到这一点(它具有自动缩放)。

现在如果你想要更多的选择,你可以使用“android:adjustViewBounds = true”和“android:scaleType”。

+0

我编辑了这个问题。我缩小了相机预览。需要将其设置为正确的宽高比。 –