Hamiltonian coupling of electromagnetic field and matter