2016-08-01 161 views
0

當對象(任何文檔)完全佔用相機邊界內的相機視圖時,從相機獲取圖片。實際上我已經創建了自定義相機,並且當圖片完全進入相機邊界時我想獲得任何對象圖片。我已經在xml部分中設置了相機邊界。 這是我的代碼,請檢查並糾正我的問題。從相機獲取圖片

public class MainActivity extends ActionBarActivity { 

    private ImageSurfaceView mImageSurfaceView; 
    private Camera camera; 
    public static FrameLayout cameraPreviewLayout; 
    private ImageView capturedImageHolder; 
    String TAG = "MainActivity"; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

     cameraPreviewLayout = (FrameLayout) findViewById(R.id.camera_preview); 
     camera = checkDeviceCamera(); 
     mImageSurfaceView = new ImageSurfaceView(MainActivity.this, camera); 
     cameraPreviewLayout.addView(mImageSurfaceView); 




     Button captureButton = (Button) findViewById(R.id.button); 
     captureButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       camera.takePicture(null, null, pictureCallback); 


      } 
     }); 
    } 

    private Camera checkDeviceCamera() { 
     Camera mCamera = null; 
     try { 
      mCamera = Camera.open(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return mCamera; 
    } 

    PictureCallback pictureCallback = new PictureCallback() { 
     @Override 
     public void onPictureTaken(byte[] data, Camera camera) { 

      File pictureFile = getOutputMediaFile(); 
      if (pictureFile == null) { 
       return; 
      } 
      try { 
       FileOutputStream fos = new FileOutputStream(pictureFile); 
       fos.write(data); 
       fos.close();; 
      } catch (FileNotFoundException e) { 

      } catch (IOException e) { 
      } 
     } 
    }; 


    private static File getOutputMediaFile() { 
     File mediaStorageDir = new File(
       Environment 
         .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), 
       "MyCameraApp"); 
     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"); 

     Log.e("mediaFile",""+mediaFile.length()); 

     return mediaFile; 
    } 


} 

這是xml文件。

<FrameLayout 
     android:id="@+id/camera_preview" 
     android:layout_width="400dp" 
     android:layout_height="320dp" 
     android:background="@drawable/camera_backround" 
     android:padding="10dp" 
     android:layout_margin="10dp" 
     > 

    </FrameLayout> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/capture_button" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     /> 



</RelativeLayout> 

這是我的SurfaceView類。

public class ImageSurfaceView extends SurfaceView implements SurfaceHolder.Callback,Camera.PictureCallback, Camera.PreviewCallback { 

    private Camera camera; 
    private SurfaceHolder surfaceHolder; 
    Context context; 
    private boolean mFocused; 
    private boolean imageProcessorBusy=true; 
    private int width; 
    private int height; 
    public void setImageProcessorBusy(boolean imageProcessorBusy) { 
     this.imageProcessorBusy = imageProcessorBusy; 
    } 


    public ImageSurfaceView(Context context, Camera camera) { 
     super(context); 
     this.camera = camera; 
     this.surfaceHolder = getHolder(); 
     this.surfaceHolder.addCallback(this); 
     this.surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
     this.context=context; 
    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     camera.setDisplayOrientation(90); 
     try { 
      this.camera.setPreviewDisplay(holder); 
      this.camera.startPreview(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
     refreshCamera(); 
     Log.e("camera", "surfaceChanged"); 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 

      Log.e("camera","surfaceDestroyed"); 
      this.camera.stopPreview(); 
      this.camera.setPreviewCallback(null); 
      this.camera.release(); 
      this.camera = null; 

    } 

    private void refreshCamera() { 
     try { 
      Log.e("camera", "refreshCamera"); 
      camera.stopPreview(); 
     } catch (Exception e) { 
     } 
     try { 
      Log.e("camera", "refreshCamera----2"); 
      camera.setPreviewDisplay(surfaceHolder); 
      camera.startPreview(); 
      camera.setPreviewCallback(this); 
     } catch (Exception e) { 


     } 
    } 


    @Override 
    public void onPictureTaken(byte[] bytes, Camera camera) { 
     android.hardware.Camera.Size pictureSize = camera.getParameters().getPictureSize(); 

     Log.d("Check", "onPictureTaken - received image " + pictureSize.width + "x" + pictureSize.height); 
    } 

    @Override 
    public void onPreviewFrame(byte[] data, Camera camera) { 

     android.hardware.Camera.Size pictureSize = camera.getParameters().getPreviewSize(); 
     // layout in the activity that the cameraView will placed in 
     int layoutWidth = MainActivity.cameraPreviewLayout.getWidth(); 
     int layoutHeight = MainActivity.cameraPreviewLayout.getHeight(); 
     Log.e("layoutWidth","------"+layoutWidth); 
     Log.e("layoutHeight", "---------" + layoutHeight); 
     Log.d("onPreviewFrame", "onPreviewFrame - received image " + pictureSize.width + "x" + pictureSize.height 
       + " focused: "+ mFocused +" imageprocessor: "+(imageProcessorBusy?"busy":"available")); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     width = MeasureSpec.getSize(widthMeasureSpec); 
     height = width; 
     setMeasuredDimension(width, width); 

    } 


} 

回答

1

你可以使用意圖捕捉來自攝像頭的圖像數據,試試這個

private static final int TAKE_PICTURE = 1000;  
private Uri imageUri; 

在你點擊鏈接

Button captureButton = (Button) findViewById(R.id.button); 
     captureButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       takePhoto(); 

      } 
     }); 

public void takePhoto() { 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg"); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, 
      Uri.fromFile(photo)); 
    imageUri = Uri.fromFile(photo); 
    startActivityForResult(intent, TAKE_PICTURE); 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode) { 
    case TAKE_PICTURE: 
     if (resultCode == Activity.RESULT_OK) { 
      Uri selectedImage = imageUri; 
      getContentResolver().notifyChange(selectedImage, null); 
      ImageView imageView = (ImageView) findViewById(R.id.ImageView); 
      ContentResolver cr = getContentResolver(); 
      Bitmap bitmap; 
      try { 
       bitmap = android.provider.MediaStore.Images.Media 
       .getBitmap(cr, selectedImage); 

       imageView.setImageBitmap(bitmap); 
       Toast.makeText(this, selectedImage.toString(), 
         Toast.LENGTH_LONG).show(); 
      } catch (Exception e) { 
       Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT) 
         .show(); 
       Log.e("Camera", e.toString()); 
      } 
     } 
    } 
} 

XML代碼

<ImageView 
    android:id="@+id/Imageview" 
    android:layout_width="400dp" 
    android:layout_height="320dp" 
    android:background="@drawable/camera_backround" 
    android:padding="10dp" 
    android:layout_margin="10dp" 
    /> 

<Button 
    android:id="@+id/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/capture_button" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    /> 

注:如果你打算拍多張照片res添加時間戳到文件名。

+0

實際上,我們希望在掃描任何文檔時獲取任何文檔的圖片,而不僅僅是信用卡。 – Umesh

+0

@umesh這將適用於任何您使用相機捕獲的文檔嘗試代碼, – SaravInfern

+0

其中我把這個代碼活動或其他地方,我想我必須得到XML內的圖像視圖。 – Umesh