We propose a quantum nondemolition measurement of the photon-number distribution for a weak cavity field with no more than two photons. The scheme is based on the resonant interaction of atoms with the cavity field, and thus the required interaction time is much shorter than that using dispersive interaction. This is important in view of decoherence. Our scheme can also be used to generate even and odd coherent states for a weak cavity field with resonant atoms.