可以将 SurfaceView 中的图像发送到服务器吗?

It is Possible to send Image from SurfaceView to Server?

我想让相机在从 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 秒捕获一次图片时,是否可以让 Surface View 向服务器发送数据?

如果可能,我必须使用什么方法或功能?

先谢谢你。

如果您要扩展 SurfaceView,请在 class.

中使用此方法
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我还没有测试过