New York City Mayor Michael R. Bloomberg is making no apologies for the “stop, question and frisk” program that a federal judge said Monday infringes on people’s rights and singles out minorities.

Speaking bluntly, the mayor said the New York City Police Department is the poster child for safety among major U.S. cities and tracks down crime where it happens.

“And they don’t worry if their work doesn’t match up to a census chart,” he said.

U.S. District Judge Shira A. Scheindlin ruled city police officers have been stopping innocent people for years — especially minorities, even when there is no reason to suspect they’d committed a crime — to search them for weapons, drugs or other goods under the program, a marquee part of Mr. Bloomberg’s anti-crime measures.

“No one should live in fear of being stopped whenever he leaves his home to go about the activities of daily life,” the judge wrote. “Those who are routinely subjected to stops are overwhelmingly people of color, and they are justifiably troubled to be singled out when many of them have done nothing to attract the unwanted attention.”

The practice treads on the Fourth Amendment because of the nature of the search and on the 14th Amendment because of how the targets were selected, she ruled.

In a scathing rebuke, she said the city’s highest officials “have turned a blind eye to the evidence that officers are conducting stops in a racially discriminatory manner,” she wrote.

For years, civil rights advocates and groups such as the American Civil Liberties Union warned that the stop-and-frisk program amounts to rampant racial profiling. The high-profile legal decision drew commentary from the top contenders to succeed Mr. Bloomberg in the city’s tabloid-friendly mayoral race, as well as the legal community.

“Today’s ruling is a vindication not just of the rights of all New Yorkers, but of all Americans,” said Jerry J. Cox, president of the National Association of Criminal Defense Lawyers.

Race weighed heavily in the judge’s reasoning. She warned of a “self-perpetuating cycle” of discrimination if the NYPD continued to use past crime data as a baseline for who it stops, and that racial disparities jump out from data.

The New York Police Department made 4.4 million stops from January 2004 to June 2012, of which 80 percent involved blacks or Hispanics, court papers said. Yet police seized weapons and contraband from whites at a higher rate than among minorities.

The number of stops per year increased dramatically, from 314,000 in 2004 to a high of 686,000 in 2011. Six percent of the stops resulted in an arrest and 6 percent in a summons, while 88 percent of the stops resulted in no further action by law enforcement.

“To be very clear: I am not ordering an end to the practice of stop and frisk,” Judge Scheindlin said, noting her decision in Floyd v. the City of New York was limited to the constitutionality of how it is conducted.

“In each of these stops a person’s life was interrupted,” she wrote. “The person was detained and questioned, often on a public street. More than half of the time the police subjected the person to a frisk.”

In a separate opinion, the judge appointed an independent monitor, Peter L. Zimroth of the Arnold & Porter law firm in New York, to ensure the program is “carried out in a manner that protects the rights and liberties of all New Yorkers, while still providing much needed police protection.”

She also ordered the NYPD to establish a pilot program that places body-worn cameras on patrol officers in one precinct per borough, namely the areas that racked up the highest number of stops in 2012. Recordings of the stops could benefit both officers and civilians, she said.

The case had proceeded to trial without a jury because the plaintiffs sought an injunction and not damages.

Mr. Bloomberg said Judge Scheindlin did not give the city a fair trial and that his office is preparing an appeal. In a defiant news conference, he and NYPD Commissioner Raymond W. Kelly argued people are misreading their efforts to save lives in poorer areas.

“We do not engage in racial profiling,” Mr. Kelly said. “It is prohibited by law, it is prohibited by our own regulations.”

Embattled mayoral candidate Anthony D. Weiner, a former House Democrat who resigned from Congress amid revelations he sent lewd photos to women over the Internet, called the ruling a “teachable moment” for the city.

“This decision sadly confirms what was profoundly obvious,” he said in a statement. “When the police stop tens of thousands of citizens who have done nothing wrong — the overwhelming number being young men of color — basic civil rights are being violated.”

City Council Speaker Christine C. Quinn also condemned the patterns outlined in the ruling.

“Today’s court ruling affirms what we’ve known,” the mayoral contender said on Twitter, “that we must put an end to young men of color being stopped in an unconstitutional manner.”