2017-10-10 3 views
-2

SurfaceView에서 캡처 한 카메라가 항상 사진을 보내길 원합니다.SurfaceView에서 Server로 이미지를 보낼 수 있습니까?

이것은 SurfaceView 용 코드입니다.

public class CustomCamera extends AppCompatActivity implements SurfaceHolder.Callback,Camera.PictureCallback, Camera.ShutterCallback { 
      Camera camera; 
      SurfaceView mPreview; 
      int count = 0; 
    @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_custom_camera); 

      mPreview = (SurfaceView) findViewById(R.id.preview); 
      mPreview.getHolder().addCallback(this); 
      mPreview.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

      camera = Camera.open(); 
      final Handler handler = new Handler(); 
      final Runnable runnable = new Runnable() { 
       @Override 
       public void run() { 

         AutomatedPicture(); 
         count++; 
       } 
      }; 
      final Timer autoCapture = new Timer(); 
      autoCapture.schedule(new TimerTask() { 
       @Override 
       public void run() { 
        if (count != 3) { 
         AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE); 
         mgr.setStreamMute(AudioManager.STREAM_SYSTEM, false); 
         handler.post(runnable); 
        }else { 
         autoCapture.cancel(); 
         finish(); 
         Toast.makeText(getApplicationContext(),"Verify failed",Toast.LENGTH_LONG).show(); 
         camera.release(); 
        } 

       } 
      },500,5000); 

     } 

     @Override 
     protected void onPause() { 
      super.onPause(); 
      camera.stopPreview(); 
     } 

     @Override 
     protected void onDestroy() { 
      super.onDestroy(); 
      camera.release(); 
      Log.d("Camera","Destroy"); 
     } 
     public void onCancelClick(View v){ 
      camera.release(); 
      finish(); 

     } 
     public void onSnapClick(View v){ 
      camera.takePicture(this, null, null, this); 
     } 
     public void AutomatedPicture(){ 
      camera.takePicture(this,null,null,this); 
     } 
@Override 
    public void onPictureTaken(byte[] data, Camera camera) { 
     try{FileOutputStream out = openFileOutput("picture"+count+".jpg", Activity.MODE_PRIVATE); 
      out.write(data); 
      out.flush(); 
      out.close(); } catch (FileNotFoundException e) { 
      Log.e("Error",e.getMessage()); 
     }catch (IOException e){ 
      Log.e("Error",e.getMessage()); 
     } 
     camera.startPreview(); 
    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     try{ 
      camera.setPreviewDisplay(mPreview.getHolder()); 
     } catch (IOException e) { 
      Log.e("Error",e.getMessage()); 
     } 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
     Camera.Parameters params = camera.getParameters(); 
     List<Camera.Size> sizes = params.getSupportedPictureSizes(); 
     Camera.Size selected = sizes.get(0); 
     params.setPreviewSize(selected.width,selected.height); 
     camera.setParameters(params); 
     camera.setDisplayOrientation(90); 
     camera.startPreview(); 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
     Log.i("PREVIew","Surface Destroyed"); 
    } 

    @Override 
    public void onShutter() { 
     AudioManager mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
     mgr.setStreamMute(AudioManager.STREAM_SYSTEM, true); 
    } 

이것은 내 XML입니다.

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 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" 
    tools:context="com.simasjiwa.sijiku.CustomCamera"> 
    <SurfaceView 
     android:id="@+id/preview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintRight_toRightOf="parent" /> 
    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="100dip" 
     android:layout_alignParentBottom="true" 
     android:gravity="center_vertical" 
     android:background="#A000">> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="100dip" 
      android:layout_height="wrap_content" 
      android:onClick="onCancelClick" 
      android:text="Cancel" /> 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="100dip" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:onClick="onSnapClick" 
      android:text="Snap Photo" /> 

    </RelativeLayout> 

</android.support.constraint.ConstraintLayout> 

제 질문은 매 5 초마다 사진을 찍을 때 표면 뷰가 서버로 데이터를 보내도록 할 수 있습니까?

가능한 경우 어떤 방법을 사용해야합니까?

감사합니다.

+0

가능한 중복 https://stackoverflow.com/questions/25091703/how-to-create-and-save-a-screenshot-from-a-surfaceview 새로운 질문을 게시하기 전에 스택 오버플로를 확인하십시오. –

답변

0

SurfaceView를 확장하는 경우 클래스 내부에서이 방법을 사용하십시오.

public Bitmap getBitmap() { 

    setDrawingCacheEnabled(true); 
    buildDrawingCache(true); 
    final Bitmap bitmap = Bitmap.createBitmap(getDrawingCache()); 
    setDrawingCacheEnabled(false); 

    destroyDrawingCache(); 
    return bitmap; 
} 

확장하지 않고, 그것과 같을 것이다 : 나는 서피스 뷰 SurfaceView를 확장하지 않고 그것을 테스트하지 않은

public Bitmap getBitmap(SurfaceView surfaceView) { 

    surfaceView.setDrawingCacheEnabled(true); 
    surfaceView.buildDrawingCache(true); 
    final Bitmap bitmap = Bitmap.createBitmap(surfaceView.getDrawingCache()); 
    surfaceView.setDrawingCacheEnabled(false); 

    surfaceView.destroyDrawingCache(); 
    return bitmap; 
} 

.