2017-10-05 152 views
0

我想要做的是,在一个活动中用SurfaceView表示打开相机,并在其中预览。点击一个按钮后,SurfaceView停止/不可见,因此图像捕捉将显示在ImageView的活动中。单击按钮时如何隐藏SurfaceView?

是如下图:

enter image description here

所以我必须具有类似下面的XML定制相机的活动,

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true"> 

<SurfaceView 
    android:id="@+id/surface_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<ImageView 
    android:id="@+id/showImage" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:visibility="invisible" /> 

<FloatingActionButton 
    android:id="@+id/cameraButton" 
    android:layout_width="60dp" 
    android:layout_height="60dp" 
    android:layout_centerHorizontal="true" /> 

</FrameLayout> 

我想要做的是,当我点击在浮动按钮上,SurfaceView消失,ImageView可见,则摄像机拍摄的图像显示在ImageView上。

所以我已经试过到目前为止

public class CameraActivity extends AppCompatActivity implements 
SurfaceHolder.Callback { 
    Camera mCamera; 
    Camera.PictureCallback jpegCallback; 

    ImageView showImage; 
    FloatingActionButton btnCamera; 
    SurfaceHolder surfaceHolder; 
    SurfaceView surfaceView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity); 

     btnCamera = (FloatingActionButton) findViewById(R.id.cameraButton); 
     surfaceView = (SurfaceView)findViewById(R.id.surface_view); 
     showImage = (ImageView)findViewById(R.id.showImage); 

     surfaceHolder = surfaceView.getHolder(); 
     surfaceHolder.addCallback(this); 
     surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
     btnCamera.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //here when click the camera button 
       //camera take photo 
       //surface view disappear 
       //preview image shown on image view 
       mCamera.takePicture(null,null,jpegCallback); 
       showImage.setVisibility(View.VISIBLE); 
       surfaceView.setVisibility(View.INVISIBLE); 

      } 
     }); 

     jpegCallback = new Camera.PictureCallback() { 
      @Override 
      public void onPictureTaken(byte[] data, Camera camera) { 
       FileOutputStream outputStream = null; 
       File file_image = getDirs(); 

       if(!file_image.exists() && !file_image.mkdirs()){ 
        Toast.makeText(getApplicationContext(),"Failed to save picture",Toast.LENGTH_LONG).show(); 
       } 

       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); 
       String date = simpleDateFormat.format(new Date()); 
       String photofile = "myphotos" + date+ ".jpg"; 
       String file_name = file_image.getAbsolutePath() + "/" + photofile; 
       File picFile = new File(file_name); 
       Bitmap bitmap = null; 

       try{ 
        outputStream = new FileOutputStream(picFile); 
        outputStream.write(data); 
        outputStream.close(); 

        //here set the picture capture to the image view 
        //convert it to bitmap,setBitmap to the imageView 
        bitmap = decodeFile(picFile,10); 

        if(bitmap !=null){ 
         showImage.setImageBitmap(bitmap); 
         Toast.makeText(CameraActivity.this, 
           "Picture Captured Successfully:", Toast.LENGTH_LONG) 
           .show(); 
        }else { 
         Toast.makeText(CameraActivity.this, 
           "Failed to Capture the picture. kindly Try Again:", 
           Toast.LENGTH_LONG).show(); 
        } 


       } catch (IOException e){ 
        e.printStackTrace(); 
       } 
       Toast.makeText(getApplicationContext(),"Picture saved",Toast.LENGTH_LONG).show(); 
       refreshCamera(); 
      } 
     }; 
    } 

    private void refreshCamera() { 
     if(surfaceHolder.getSurface() == null){ 
      return; 
     } 

     //stop the camera preview 
     try{ 
      mCamera.stopPreview(); 
     }catch (Exception e){ 
      e.printStackTrace(); 
     } 
     //start camera again 
     try{ 
      mCamera.setPreviewDisplay(surfaceHolder); 
      mCamera.startPreview(); 
     }catch (Exception e){ 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     //open camera 
     try{ 
      mCamera = Camera.open(); 
     }catch (RuntimeException ex){ 
      ex.printStackTrace(); 
     } 

     Camera.Parameters parameters; 
     parameters = mCamera.getParameters(); 
     parameters.setPreviewFrameRate(20); 
     parameters.setPreviewSize(352,288); 
     mCamera.setParameters(parameters); 
     mCamera.setDisplayOrientation(90); 

     try{ 
      mCamera.setPreviewDisplay(surfaceHolder); 
      mCamera.startPreview(); 
     }catch (Exception e){ 
      e.printStackTrace(); 
     } 


    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
     refreshCamera(); 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
     mCamera.stopPreview(); 
     mCamera.release(); 
     mCamera= null; 
    } 
    } 

试图在此之后,我现在得到的是,屏幕是完全空白。 SurfaceView是不可见的,ImageView预览从相机拍摄的图像未显示出来。 但我检查了file_namebitmap在日志中有价值。

所以,我试图以不SurfaceView设置为不可见象下面这样:

btnCamera.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      cameraImage(); 
      showImage.setVisibility(View.VISIBLE); 
      //surfaceView.setVisibility(View.INVISIBLE); 

     } 
    }); 

图像可以显示在上表面视图的顶部等的下方,但如果我设置面视图为不可见,整个屏幕显示为空白。

enter image description here

但我想表面视线中消失,只有ImageView的使用。

回答

0

移动以下行(使表面视图隐形

surfaceView.setVisibility(View.INVISIBLE); 

onClick(View v)方法onPictureTaken(byte[] data, Camera camera)方法可以解决这个问题。

但是,这将导致相机release,它必须再次打开捕捉另一张照片。

+0

当我使surfaceView不可见时,屏幕变为空白..屏幕上只显示白色 – ken