我尝试在SurfaceView中显示相机预览并将该预览上传到我的网站。有点像无线网络摄像头。预览在surfaceView中显示正常,但它似乎无法上传到我的网站,我的网站没有收到任何内容。这是我在android中的代码
public class MainActivity extends AppCompatActivity {
SurfaceView surfaceView;
SurfaceHolder holder;
private Camera camera;
String MyImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
camera.setPreviewCallback(new Camera.PreviewCallback()
{
public void onPreviewFrame(final byte[] data, final Camera camera)
{
MyImage = Base64.encodeToString(data, Base64.DEFAULT);
final ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",MyImage));
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);
}catch(Exception e){e.printStackTrace();}
}
});
t.start();
}
});
}
catch (IOException e) {e.printStackTrace();}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
}
});
}
}
在网站上接收预览的和.php文件
<?php
$base=$_REQUEST['image'];
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('uploaded_image.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
$path = "publib_html/";
$tmp_name = $_FILES['file']['tmp_name'];
$name = $_FILES['file']['name'];
$type = $_FILES['file']['type'];
$size = $_FILES['file']['size'];
move_uploaded_file($tmp_name,$path.$name);
?>
答案 0 :(得分:0)
预览图像通常不是JPEG图像,而您的服务器代码似乎暗示您认为它们是。实际格式取决于您提供给setPreviewFormat()
on Camera.Parameters
的内容,并且必须是getSupportedPreviewFormats()
中的值之一。最有可能的是NV21
或YV12
。