Reputation: 157
Alright so I am working on an app that has to take pictures and send them to a server for processing. I need this for some image recognition that will eventually help control a robot. Basically I need to use the android device as a webcam that sends pictures. I figured out the Sockets part but now after fiddling with some code for a few days I ended up with this:
public class MainActivity extends AppCompatActivity {
public static final String dTag = "DBG";
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button trg = (Button)findViewById(R.id.trigger_btn);
trg.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v)
{
SurfaceHolder mSurfaceHolder = new SurfaceHolder() {
@Override
public void addCallback(Callback callback) {
}
@Override
public void removeCallback(Callback callback) {
}
@Override
public boolean isCreating() {
return false;
}
@Override
public void setType(int type) {
}
@Override
public void setFixedSize(int width, int height) {
}
@Override
public void setSizeFromLayout() {
}
@Override
public void setFormat(int format) {
}
@Override
public void setKeepScreenOn(boolean screenOn) {
}
@Override
public Canvas lockCanvas() {
return null;
}
@Override
public Canvas lockCanvas(Rect dirty) {
return null;
}
@Override
public void unlockCanvasAndPost(Canvas canvas) {
}
@Override
public Rect getSurfaceFrame() {
return null;
}
@Override
public Surface getSurface() {
return null;
}
};
Camera.PictureCallback mCall = new Camera.PictureCallback()
{
@Override
public void onPictureTaken(byte[] data, Camera camera)
{
ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
ImageView imW = (ImageView)findViewById(R.id.imView);
imW.setImageBitmap(bitmap);
Log.d(dTag, "" + data.length);
}
};
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Camera mCamera;
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay();
mCamera.startPreview();
}
catch(IOException e){
Log.d(dTag, "Cam is null!");
}
mCamera.takePicture(null, null, mCall);
mCamera.stopPreview();
mCamera.release();
}
});
}
Now whenever I press the button I see this in the debug log "D/Camera: app passed NULL surface", I assume this is because of mSurfaceHolder which isn't properly declared. If anyone could point to me what the problem is and how to solve it I would be grateful since I don't have a very good understanding of java and can't seem to find anything that works on the internet.
Upvotes: 0
Views: 100
Reputation: 5951
several problems -
You have no surface view and surface holder must be obtained from the surface view. You just can not create a "new" for this purpose.
You did not pass any surfaceolder
in mCamera.setPreviewDisplay();
so system can not decide where to display.
Your method local anonymous inner class is just simply wrong.
Tutorial link: http://examples.javacodegeeks.com/android/core/ui/surfaceview/android-surfaceview-example/
Upvotes: 2