我尝试在chplay中编写像ipcam app这样的应用程序(使用我的智能手机就像无线网络摄像头),我使用相机API捕获1张图片并将其发送到网站确定。
但现在,我想拍摄照片并不断将它们发送到我的网站。通常情况下,我使用一个按钮来捕获图像,使用一个按钮上传图像,但是当我不使用按钮时,它将无法运行。
如何解决此问题并将图片连续流式传输到我的网站?
这是我的所有代码:
public class MainActivity extends AppCompatActivity {
private Camera camera;
private LinearLayout scrollImages;
Button btnCapture, btnSave, btnUpload, btnExit;
SurfaceView surfaceView;
SurfaceHolder holder;
Bitmap bitmap, scaledBitmap, rotatedBitmap;
String path, MyImage_Encode;
byte[] MyImage;
boolean tt = false;
ImageView imageView;
InputStream inputStream;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollImages = (LinearLayout)findViewById(R.id.scrollImages);
btnCapture = (Button) findViewById(R.id.btnCapture);
btnSave = (Button) findViewById(R.id.btnSave);
btnUpload = (Button) findViewById(R.id.btnUpload);
btnExit = (Button) findViewById(R.id.btnExit);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
holder = surfaceView.getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
camera = camera.open();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
camera.setDisplayOrientation(90);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
}
});
btnCapture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
camera.takePicture(null, null, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
Matrix matrix = new Matrix();
matrix.postRotate(90);
scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
imageView = new ImageView(getApplicationContext());
imageView.setImageBitmap(rotatedBitmap);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bao);
MyImage = bao.toByteArray();
}
});
}
});
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(MyImage);
fos.close();
Toast.makeText(MainActivity.this, "New Image saved", Toast.LENGTH_LONG).show();
} catch (Exception error) {
Toast.makeText(MainActivity.this, "Image could not be saved.", Toast.LENGTH_LONG).show();
}
}
});
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyImage_Encode = Base64.encodeToString(MyImage, Base64.DEFAULT);
final ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",MyImage_Encode));
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://nns12151069.esy.es/upload_image.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Image Upload Successful", Toast.LENGTH_LONG).show();
}
});
} catch(Exception e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Image Upload Failed", Toast.LENGTH_LONG).show();
}
});
}
}
});
t.start();
}
});
LinearLayout layoutBackground = (LinearLayout) findViewById(R.id.background);
layoutBackground.setOnClickListener(new LinearLayout.OnClickListener() {
@Override
public void onClick(View v) {
camera.autoFocus(myAutoFocusCallback);
}
Camera.AutoFocusCallback myAutoFocusCallback = new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
}
};
});
}
}
答案 0 :(得分:0)
拍摄照片时,相机预览会自动停止。如果您没有运行相机预览,则无法拍照。
因此,您应该在每次takePicture操作后重新开始预览。
答案 1 :(得分:0)
我建议只发送预览帧。看看PreviewCallback
http://developer.android.com/reference/android/hardware/Camera.PreviewCallback.html