2017-10-10 81 views
-2

我想讓相機在從SurfaceView捕捉時始終發送圖片。可以將圖像從SurfaceView發送到服務器?

這是我的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; 
} 

不延長,它應該是這樣的:

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; 
} 

我沒有不延長SurfaceView進行了測試。