It seems to me you are rewarding him with the treats to keep acting this way. When my girl was about that age she did the same things outside. I would have her sit and calm down after awhile she realized there was no reason to bark at everything. When nothing was going on I worked on her sit and attention with treat rewards so her good behavior was rewarded.