2017-10-05 17 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の。 onPictureTaken(byte[] data, Camera camera)方法にonClick(View v)方法から、次の行(表面図を非表示にする)を移動

答えて

0

surfaceView.setVisibility(View.INVISIBLE); 

は、問題を解決するだろう。

しかし、それはカメラreleaseになり、別の画像をキャプチャするために再び開く必要があります。

+0

surfaceViewを非表示にすると、画面が空白になります。画面上に白い色しか表示されません。 – ken

関連する問題