2017-10-05 187 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