  Number-resolving single-photon detectors represent a key technology for a
host of quantum optics protocols, but despite significant efforts,
state-of-the-art devices are limited to few photons. In contrast,
state-dependent atom counting in arrays can be done with extremely high
fidelity up to hundreds of atoms. We show that in waveguide QED, the problem of
photon counting can be reduced to atom counting, by entangling the photonic
state with an atomic array in the collective number basis. This is possible as
the incoming photons couple to collective atomic states and can be achieved by
engineering a second decay channel of an excited atom to a metastable state.
Our scheme is robust to disorder and finite Purcell factors, and its fidelity
increases with atom number. Analyzing the state of the re-emitted photons, we
further show that if the initial atomic state is a symmetric Dicke state,
dissipation engineering can be used to implement a nondestructive photon-number
measurement, in which the incident state is scattered into the waveguide
unchanged. Our results generalize to related platforms, including
superconducting qubits.
